0

Here is an example of how to get column based row values in an ObservableCollection "OrderCollection":

Double OrderSubTotal = (Double)0;

foreach (var data in this.OrderCollection)
{
    Order orderData = (Order)data;
    //MessageBox.Show(orderData.Product.SalesPrice.ToString());
    OrderSubTotal = orderData.Product.SalesPrice;
}

this.OrderSum += OrderSubTotal;
user3524628
  • 137
  • 1
  • 3
  • 14
  • You don't "loop thru a DataGrid" in WPF because [UI is NOT Data](http://stackoverflow.com/a/14382137/643085). It is strongly recommended that you create a proper DataModel / ViewModel and use proper DataBinding instead of hacking stuff out of the UI. – Federico Berasategui Apr 28 '14 at 16:21
  • what for? WPF is intended to be used with DataBinding, not with horrible winforms-like hacks. There is absolutely no need for this. – Federico Berasategui Apr 28 '14 at 16:39
  • simply read the values from your data model. There's no need to "read" the UI, since you already have a Bound DataModel. – Federico Berasategui Apr 28 '14 at 17:00

2 Answers2

0
for (int i = 0; i < dataGrid.Items.Count; i++ )
{
 DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
 foreach (var gridColumn in dataGrid.Columns)
 {
  if(gridColumn.Header == "Something")
  {
    // Do something
  }
}
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
0
foreach(object t in PaymentDataGrid.SelectedItems)
{
    DataRowView row = t as DataRowView;
    if (row != null)
    {
        var transactionId = (int)row[0];
    }
}

this code worked perfectly to retrieve the specified column index for each selected row.

Simon Hughes
  • 3,534
  • 3
  • 24
  • 45
wsduho
  • 377
  • 3
  • 9