Vitaly
Vitaly

Reputation: 83

Access a cell in DataGrid.SelectedItems

I need to read a string from each of the the first cells of SelectedItems of the DataGrid

foreach (var item in myDataGrid.SelectedItems)
{
    if (item[0].ToString().Contains("Buy"))
    {
        containsBuy = true;
    }

    if (item[0].ToString().Contains("Sell"))
    {
        containsSell = true;
    }
}

How can I cast myDataGrid.SelectedItems? It is IList and gives object. Is there any simple and similar way to do it as it is done for one selected row of a DataGrid:

var row = myDataGrid.SelectedItem as DataRowView;

Here I can easy access any cell - row[i].

Upvotes: 1

Views: 666

Answers (1)

DaveShaw
DaveShaw

Reputation: 52788

Just change your foreach loop to:

foreach (DataRowView in myDataGrid.SelectedItems)
{
    //...
}

Upvotes: 1

Related Questions