hello all

suppose I have fill a datagridview (named it mydgv)
,Can I populate a dataset with the data from the content of mydgv ?
if yes how


thank you

denny

Just tell me how would you want to populate the dataset( means do you want to populate the dataset row by row of datagridview or do you want to populate the whole dataset from the datagridview at single time)?

hi Shridarmaster

Don't care about row by row or whole in a time
both will be usefull to me

thank you
denny

1st you need to create columns, then you fill the row (by looping through rows of dgv):

DataSet ds = new DataSet();
DataTable table = new DataTable("DataFromDGV");
//if you have only one table, it pointless to create a dataSet
//dataSet is used as a collection of more-then-one dataTable
//so you can do:
ds.Tables.Add(table);
//create columns:
foreach(DataGridViewColumn col in dgv.Columns)
     table.Columns.Add(col.HeaderText, typof(string));
//fill the rows of table:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
     foreach (DataGridViewCell cell in row.Cells)
     {
          table.Rows[row.Index][cell.ColumnIndex] = cell.Value;   
     }          
}

Edited 5 Years Ago by Mitja Bonca: n/a

This question has already been answered. Start a new discussion instead.