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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jean PaulRank #32 · 38.5K reputationPosted Dec 16, 2010, 6:27 AM EST
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 DeRank #1,108 · 1.7K reputationPosted Dec 16, 2010, 1:02 AM EST
Thanks.....
Sam HobbsRank #107 · 20.5K reputationPosted Dec 16, 2010, 12:10 PM EST
Neerav Sachdeva33 reputationPosted Dec 19, 2010, 8:47 PM EST
// 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.
Subhendu DeRank #1,108 · 1.7K reputationPosted Dec 19, 2010, 8:52 PM EST
while(_reader.Read())
{
table.Rows.Add(new object[] { Convert.ToInt32(_reader[0]), Convert.ToString(_reader[1])});
}
Thanks.....