Looping through Dataset instead of DataGridView?
-
Hi, I would like to loop through the DataSet at runtime to retrieve selected values and store them in local variables but i am not sure how i could do this using DataSet that is generated using the inbuilt wizard. I am using SQL Server Compact. My database has "*.sdf" extension. I created a strongly typed DataSet using the wizard. So in the solution explorer i can see the DataSet and it has an extension of "*.xsd". At the moment i am doing this:
foreach(DataGridViewRow dgvrow in CustomerGridView.Rows)
{
//e.g to get surname
String Surname = dgvrow.Cells[2].Value.ToString();
//do some other stuff here too}
Wouldnt it be more efficient if i looped through the DataSet that I have created? If so, how would i go about doing this based on the above code. If anybody could assist me with this, it would be much appreciated. Thanks,
-
Hi, I would like to loop through the DataSet at runtime to retrieve selected values and store them in local variables but i am not sure how i could do this using DataSet that is generated using the inbuilt wizard. I am using SQL Server Compact. My database has "*.sdf" extension. I created a strongly typed DataSet using the wizard. So in the solution explorer i can see the DataSet and it has an extension of "*.xsd". At the moment i am doing this:
foreach(DataGridViewRow dgvrow in CustomerGridView.Rows)
{
//e.g to get surname
String Surname = dgvrow.Cells[2].Value.ToString();
//do some other stuff here too}
Wouldnt it be more efficient if i looped through the DataSet that I have created? If so, how would i go about doing this based on the above code. If anybody could assist me with this, it would be much appreciated. Thanks,
-
Hi, Thanks but do i not need to modify what is in between the brackets?
foreach(DataGridViewRow dgvrow in CustomerGridView.Rows)
Also how would i access the value from the specified index? Thanks,
well offcourse you do. Either you loop the dvg or the ds.
foreach (DataRow r in ds.Tables["TableName"].Rows)
{
String surname = r.ItemArray[2].ToString();
}but I'm really not sure that it will be faster. If you want fast forget about ds and dvg(sure use one for the UI display) and use a
List<MyClass>
set thedgv.DataSource = myList;
and then Loop throuh the List or use linq or .... [Added] the DataRow.ItemArray returns an array of objects => many casts/boxing/unboxim => poor perf On the other hand if you load the data in a List using a datareader => no casting and faster when loading data, and especially faster when looping/searching/whatever on the List/Datas.[/Added]modified on Wednesday, March 10, 2010 6:40 PM
-
well offcourse you do. Either you loop the dvg or the ds.
foreach (DataRow r in ds.Tables["TableName"].Rows)
{
String surname = r.ItemArray[2].ToString();
}but I'm really not sure that it will be faster. If you want fast forget about ds and dvg(sure use one for the UI display) and use a
List<MyClass>
set thedgv.DataSource = myList;
and then Loop throuh the List or use linq or .... [Added] the DataRow.ItemArray returns an array of objects => many casts/boxing/unboxim => poor perf On the other hand if you load the data in a List using a datareader => no casting and faster when loading data, and especially faster when looping/searching/whatever on the List/Datas.[/Added]modified on Wednesday, March 10, 2010 6:40 PM
Not really. You can loop through properties of your tables since you have typed dataset.
foreach(MyTableRow row in MyTable.Rows)
string surname = row.SurName; -
Not really. You can loop through properties of your tables since you have typed dataset.
foreach(MyTableRow row in MyTable.Rows)
string surname = row.SurName; -
Hi, Just a quick question regarding best practice, is it best to loop through the rows like you have stated instead of looping through the datagridview? Thanks for the input all.
Basically depends on what you want to achieve. In case it is looping through all visible rows in datagridview (In case you are using DataView) then DataSet is of no help. In case it is a basic looping through all the rows, why bother going to datagridview? you need to cast the object back to your datatype. Better would be to loop through TYPED datarow through a TYPED datatable... typed datarow already takes care of the conversion.