C#:How to navigate through records in a datatable/dataset
-
hi all i have a form with First,Next,Prev,Buttons. i had populated a datatable with datas .Now i need to navigate through the records , can any body help me how to achieve this.
conn = new SqlConnection(connstr); conn.Open(); SqlCommand cmd = new SqlCommand(sqlAllCustomers, conn); SqlDataAdapter adpt = new SqlDataAdapter(cmd); DataTable dtCust1 = new DataTable("CustomerDatTab"); adpt.Fill(dtCust1);
thankssenthil
-
hi all i have a form with First,Next,Prev,Buttons. i had populated a datatable with datas .Now i need to navigate through the records , can any body help me how to achieve this.
conn = new SqlConnection(connstr); conn.Open(); SqlCommand cmd = new SqlCommand(sqlAllCustomers, conn); SqlDataAdapter adpt = new SqlDataAdapter(cmd); DataTable dtCust1 = new DataTable("CustomerDatTab"); adpt.Fill(dtCust1);
thankssenthil
dtCust1.Rows[index]["columnName"]
SkyWalker
-
hi all i have a form with First,Next,Prev,Buttons. i had populated a datatable with datas .Now i need to navigate through the records , can any body help me how to achieve this.
conn = new SqlConnection(connstr); conn.Open(); SqlCommand cmd = new SqlCommand(sqlAllCustomers, conn); SqlDataAdapter adpt = new SqlDataAdapter(cmd); DataTable dtCust1 = new DataTable("CustomerDatTab"); adpt.Fill(dtCust1);
thankssenthil
This might not be the best of the methods but I use to do like that dtCust1.Rows[i][columnName] --> this will give you the value in the datatable for the row "i" and column "columnName". Now for the next record increment the value of "i" by one, for previous record decrement the value of "i" by one, for First record set the value of "i" to 0 and for Last record set the value of "i" to dtCust1.Rows.Count. Hope this helps. Regards
-
hi all i have a form with First,Next,Prev,Buttons. i had populated a datatable with datas .Now i need to navigate through the records , can any body help me how to achieve this.
conn = new SqlConnection(connstr); conn.Open(); SqlCommand cmd = new SqlCommand(sqlAllCustomers, conn); SqlDataAdapter adpt = new SqlDataAdapter(cmd); DataTable dtCust1 = new DataTable("CustomerDatTab"); adpt.Fill(dtCust1);
thankssenthil
You could use either:
for (int i = 0; i < dtCust1.Rows.Count; i++) { DataRow row = dtCust.Rows[i]; Console.WriteLine(row[0]); // This would print the first column in the each row. }
or
foreach (DataRow row in dtCust1.Rows) { Console.WriteLine(row[0]); // This would print the first column in the each row. }
Deja View - the feeling that you've seen this post before.
-
This might not be the best of the methods but I use to do like that dtCust1.Rows[i][columnName] --> this will give you the value in the datatable for the row "i" and column "columnName". Now for the next record increment the value of "i" by one, for previous record decrement the value of "i" by one, for First record set the value of "i" to 0 and for Last record set the value of "i" to dtCust1.Rows.Count. Hope this helps. Regards