如何遍历DataGrid并使用列标题获取列值?

时间:2014-04-28 15:29:26

标签: c# wpf datagrid

以下是如何在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;

2 个答案:

答案 0 :(得分: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
  }
}

答案 1 :(得分:0)

foreach(object t in PaymentDataGrid.SelectedItems)
{
    DataRowView row = t as DataRowView;
    if (row != null)
    {
        var transactionId = (int)row[0];
    }
}

这段代码非常适合检索每个选定行的指定列索引。