Data table in C#
I have a table in my database ,i want to retrive data row wise from the table.And i want to store row wise data into a list of list .How can i do that.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jean PaulPosted Dec 16, 2010, 11:27 AM
Probably I have understood correct.. Please find the code below
// Create table
DataTable table = new DataTable();
// Create columns
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Name", typeof(string));
// Add data
table.Rows.Add(new object[] { 1, "abc" });
table.Rows.Add(new object[] { 2, "xyz" });
// Add to list
IList list = new ArrayList();
foreach (DataRow row in table.Rows)
list.Add(row);
// Print the list
foreach (DataRow row in list)
Console.WriteLine(row["Id"] + " " + row["Name"]);
(mark as answer if correct, else let me know)
Regards..
Subhendu DePosted Dec 20, 2010, 1:52 AM
while(_reader.Read())
{
table.Rows.Add(new object[] { Convert.ToInt32(_reader[0]), Convert.ToString(_reader[1])});
}
Thanks.....
Neerav SachdevaPosted Dec 20, 2010, 1:47 AM
// Add data
table.Rows.Add(new object[] { 1, "abc" });
table.Rows.Add(new object[] { 2, "xyz" });
I want to add my data not manually but I want to take it from my database.How can i do it.
Sam HobbsPosted Dec 16, 2010, 5:10 PM
Subhendu DePosted Dec 16, 2010, 6:02 AM
Thanks.....