如何在DataGrid中循环并获取具有列标题的列值

本文关键字:标题 获取 DataGrid 循环 | 更新日期: 2023-09-27 18:00:28

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

如何在DataGrid中循环并获取具有列标题的列值

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
  }
}
foreach(object t in PaymentDataGrid.SelectedItems)
{
    DataRowView row = t as DataRowView;
    if (row != null)
    {
        var transactionId = (int)row[0];
    }
}

这段代码可以很好地检索每个选定行的指定列索引。