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
.
views:
36answers:
2
+2
A:
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);
josephj1989
2010-06-26 23:53:05
+1
A:
DataSet myDataset = new DataSet();
DataTable customers = myDataset.Tables.Add("Customers");
customers.Columns.Add("Name");
customers.Columns.Add("Age");
customers.Rows.Add("Chris", "25");
//Get data
DataTable myCustomers = myDataset.Tables["Customers"];
DataRow currentRow = null;
for (int i = 0; i < myCustomers.Rows.Count; i++)
{
currentRow = myCustomers.Rows[i];
listBox1.Items.Add(string.Format("{0} is {1} YEARS OLD", currentRow["Name"], currentRow["Age"]));
}
Chris Schroeder
2010-06-27 00:02:10