Friday, October 30, 2009

How To DataGridview To Dataset Code Sample.

      We need to create the datacolumn from the datagridview column.Add created Column in to datatable .Add the datatable in to dataset.loop through the datagridview add data to datatable .

DataTable table = new DataTable("mytable");

DataColumn column;

DataRow row;

DataSet dataset = new DataSet();

column = new DataColumn();

column.DataType = dataGridView1.Columns[0].ValueType;

column.ColumnName = dataGridView1.Columns[0].Name;


column = new DataColumn();

column.DataType = dataGridView1.Columns[1].ValueType;

column.ColumnName = dataGridView1.Columns[1].Name;



for (int rowCount = 0; rowCount < dataGridView1.Rows.Count - 1; rowCount++)


row = table.NewRow();

row[0] =Convert.ToInt32(dataGridView1.Rows[rowCount].Cells[0].Value);

row[1] = dataGridView1.Rows[rowCount].Cells[1].Value.ToString();



return dataset;