DataGrid

wyrd

Well-known member
Joined
Aug 23, 2002
Messages
1,408
Location
California
I was all excited when I saw the spiffy little features it had for displaying errors etc when bound to a DataSet. I started looking for something that allowed me to loop through the selected rows (if multiple are selected), but there wasnt anything that I found. :( Is there a way? If not, then darnit *cringingly looks at the ListView control*
 
Yeah okay so my brain isnt thinking today. :p

I guess I should of said, is there a way to loop through the DataGrids rows using foreach? Right now the only way I can think of doing it is looping through the DataSet its bound to with a for() loop, then using the incremented variable to check each row index with.

C#:
DataRowCollection r = ds.Tables["StuffTable"].Rows;
for (int i = 0; i < r.Count; i++) {
	if (dataGrid1.IsSelected(i)) {
		Console.WriteLine(i + " - " + r[i]["ID"].ToString());
	}
}
 
I havent tested it, but you could try:
C#:
DataRowCollection r = ds.Tables["StuffTable"].Rows;

foreach (DataRow dr in r) {
  //do stuff with the datarow
}
 
Well, yes while that might work for looping through each DataRow in a DataSet, it doesnt loop through the DataGrid. :) The IsSelected() method only takes an Integer, so unless DataGrid has a Row collection that I couldnt find, it seems like the only way to check what rows are selected in a DataGrid is to loop using a counter.
 
Back
Top