Foreach with DataGrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello, i want to "run" on my DataGrid (in C#) on each row
i tried to do a foreach loop but i got erros, what is the correct syntax??

thanks...
 
hello,
what i mean is that i want to take from each row and cell it's text and print it.
i need to do something like dataGrid[row,cell].ToString();

thanks

Dmitriy Lapshin said:
Hi,

Do you mean the DataGrid rows or the underlying data rows? Could you please
elaborate on what you're trying to achieve?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Gidi said:
hello, i want to "run" on my DataGrid (in C#) on each row
i tried to do a foreach loop but i got erros, what is the correct syntax??

thanks...
 
Gidi,

I am not sure you can iterate on rows... , but you can easily access cells!
Here's an example from MSDN which does just what you need:

private void PrintCellValues(DataGrid myGrid){
int iRow;
int iCol;
DataTable myTable;
// Assumes the DataGrid is bound to a DataTable.
myTable = (DataTable) dataGrid1.DataSource;
for(iRow = 0;iRow < myTable.Rows.Count ;iRow++) {
for(iCol = 0;iCol < myTable.Columns.Count ;iCol++) {
Console.WriteLine(myGrid[iRow, iCol]);
}
}
}


--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Gidi said:
hello,
what i mean is that i want to take from each row and cell it's text and print it.
i need to do something like dataGrid[row,cell].ToString();

thanks

Dmitriy Lapshin said:
Hi,

Do you mean the DataGrid rows or the underlying data rows? Could you please
elaborate on what you're trying to achieve?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Gidi said:
hello, i want to "run" on my DataGrid (in C#) on each row
i tried to do a foreach loop but i got erros, what is the correct syntax??

thanks...
 
You can iterate the Items collection of the DataGrid, which will only
contain databound rows (no headers, footers, or seperators).
The Items collection will have DataGridItem objects, which in turn
have a Cells property.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top