Search code examples
c#datagridviewdatatable

C# create table from datagrid (only 2 fields)


but cant find any information. Restricted on time.

I have a Datagridview, 6 fields.

I need to make a Datatable that only contains 2 of those fields.

fields are part and pareto.

so I need all the records but only want 2 of the fields in my Datatable.

Using c sharp .net 4.0 and Microsoft visual studio 2010


Solution

  • foreach( DataGridViewRow row in myDataGridView.Rows)
    {
          DataRow tableRow = myDataTable.NewRow();
          tableRow.Cells["part"].value = row["part"].value;
          tableRow.Cells["pareto"].value = row["pareto"].value;
          myDataTable.Rows.Add(tableRow);
    }
    

    Something like this should do it. Just make sure your DataTable has the appropriate rows.