title: How to add gridview rows to a datatable?
body: <p>I have a gridview which will contain some 'n' number of rows.... Now i want to add all rows of  the gridview to a datatable which will be used for bulkcopy operation...</p>
  
  <p>I have found this <a href="http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx</a></p>
  
  <p>But i want all columns of my gridview to be added to the datarow of the datatable
  <a href="http://img85.imageshack.us/img85/4044/gridp.jpg" rel="nofollow noreferrer">Grid http://img85.imageshack.us/img85/4044/gridp.jpg</a></p>
  
  <p>I want to convert gridview to datatable on submit.... Any suggestion...</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Answer below works and i have found an answer too...</p>
  
  <pre><code>    DataTable dt = new DataTable();
      dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
      dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
      dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
      dt.Columns.Add(new DataColumn("DaysPresent", typeof(double)));
      dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
      dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
      dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
      dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
      dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
  
      foreach (GridViewRow row in gridEmployee.Rows) 
      {
          if (row.RowType == DataControlRowType.DataRow)
          {
              DataRow dr = dt.NewRow();
              dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
              dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
              dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
              dr["DaysPresent"] = Convert.ToDouble(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);
              dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
              dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
              dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
              dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
              dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
              dt.Rows.Add(dr);
          }
      }
      SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
      sbc.DestinationTableName = "SalaryDetails";
      sbc.ColumnMappings.Add("EmpId", "EmpId");
      sbc.ColumnMappings.Add("FromDate", "FromDate");
      sbc.ColumnMappings.Add("ToDate", "ToDate");
      sbc.ColumnMappings.Add("DaysPresent", "DaysPresent");
      sbc.ColumnMappings.Add("OpeningAdvance", "OpeningAdvance");
      sbc.ColumnMappings.Add("AdvanceDeducted", "AdvanceDeducted");
      sbc.ColumnMappings.Add("RemainingAdvance", "RemainingAdvance");
      sbc.ColumnMappings.Add("SalaryGiven", "SalaryGiven");
      sbc.ColumnMappings.Add("CreatedDate", "CreatedDate");
      sbc.WriteToServer(dt);
      sbc.Close();
  </code></pre>