从WPF中的DataGridRow中获取列值

本文关键字:获取 DataGridRow WPF 中的 | 更新日期: 2023-09-27 17:54:04

我正在创建一个WPF应用程序,其中当用户单击DataGrid的一行时,我需要获取列值并使用该值从数据库获取数据。

我能够找到DataGridRow,但无法获得列值。这是我的代码…

DataGridRow BillRow = sender as DataGridRow;

我将选中的行详细信息放入BillRow(我能够在Visualiser中看到它们),但无法将值放入变量中。你能帮我吗?

从WPF中的DataGridRow中获取列值

下面的解决方案可能会对您有所帮助

 public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
    {
        DataGridRow rowContainer = GetRow(dataGrid, row);
        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // now try to bring into view and retreive the cell
                dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
     }