List of Exercises

Convert DataGridView to DataTable in C#

	
   private void button1_Click(object sender, EventArgs e)
    {
      try
      {
        if (dataGridView1.Rows.Count > 1)
        {
          DataTable dt = DataGridView_To_Datatable(dataGridView1);
          var name1 = dt.Rows[0][1];
          MessageBox.Show("Data is Converted!");
        }
      }
      catch (Exception ex) { MessageBox.Show(ex.Message); }
    }
    public static DataTable DataGridView_To_Datatable(DataGridView dg)
    {
      DataTable ExportDataTable = new DataTable();
      foreach (DataGridViewColumn col in dg.Columns)
      {
        ExportDataTable.Columns.Add(col.Name);
      }
      foreach (DataGridViewRow row in dg.Rows)
      {
        DataRow dRow = ExportDataTable.NewRow();
        foreach (DataGridViewCell cell in row.Cells)
        {
          dRow[cell.ColumnIndex] = cell.Value;
        }
        ExportDataTable.Rows.Add(dRow);
      }
      return ExportDataTable;
    }
	


Download the project of Visual Studio 2013 in DropBox Download


How to Convert DataGridView to DataTable C# | Winforms


Video Thumbnail