Search code examples
c#wpfdatagridvisual-studio-2012wpfdatagrid

Looping through DataSet table


I have installed VS 2012 after many years using VS 2010 and I can't even manage to loop through table rows anymore (which was a doddle before) and it is absolutely critical to my project.

I tried many ways of calling DataGrid.Rows but that is no longer available to users at all, so I am trying to find a work around.

Here's and here is what i have come up with , but the second part is just not working for me.

foreach (TableRow row in MyDatabaseDataSet.Tables["Table1"].Rows)
{
//Pesky lengthy non-working code supposed to generate 
//controls dependent on row cell value
}

Just to make things simpler (the code not working is just so lengthy it would be counter-constructive to post it here. Instead let's try and get a messagebox to show the value of the cell in the 2nd column of the row looked at.

But nothing does it.

i tried many many ways that i could think of including:

MessageBox.Show(row.Cells[2].Value);
MessageBox.Show(row.Cells[2].Text);

But it just will not work for me at all!

Doesn't even want to run the project at all.

How can I loop through all the rows in a DataTable or even a Datagrid?

PS: I forgot to mention but I also added the Load Data segment in the code prior to the foreach statement written above.


Solution

  • Maybe i'm overlooking something simple, but DataTable.Rows returns a DataRowCollection which contains DataRows not TableRows.

    So instead of

    foreach (TableRow row in MyDatabaseDataSet.Tables["Table1"].Rows)
    {
        // ...
    }
    

    this:

    foreach (DataRow row in MyDatabaseDataSet.Tables["Table1"].Rows)
    {
        // ...
    }