Adding rows to dataset

sikas picture sikas · Jun 27, 2010 · Viewed 110.1k times · Source

How can I create a DataSet that is manually filled? ie. fill through the code or by user input. I want to know the required steps if I need to create a DataTable or a DataRow first, I really don't know the steps to fill the DataSet.

Answer

josephj1989 picture josephj1989 · Jun 27, 2010
 DataSet ds = new DataSet();

 DataTable dt = new DataTable("MyTable");
 dt.Columns.Add(new DataColumn("id",typeof(int)));
 dt.Columns.Add(new DataColumn("name", typeof(string)));

 DataRow dr = dt.NewRow();
 dr["id"] = 123;
 dr["name"] = "John";
 dt.Rows.Add(dr);
 ds.Tables.Add(dt);