DataGrid

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
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 wasn't anything that I found. :( Is there a way? If not, then darnit *cringingly looks at the ListView control*
 
Yeah okay so my brain isn't 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 it's 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 haven't 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 doesn't loop through the DataGrid. :) The IsSelected() method only takes an Integer, so unless DataGrid has a Row collection that I couldn't find, it seems like the only way to check what rows are selected in a DataGrid is to loop using a counter.
 
Back
Top