从 WPF 中的 DataGridRow 获取列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14397291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Column value from DataGridRow in WPF
提问by Krishna Thota
I'm creating a WPF application in which when a user clicks on a Row of the DataGrid, I need to take a Column value and using that value I need to get data from Database.
我正在创建一个 WPF 应用程序,当用户单击 DataGrid 的一行时,我需要获取一个 Column 值并使用该值从数据库中获取数据。
I'm able to Find the DataGridRow but unable to get the column values. Here is my code ...
我能够找到 DataGridRow 但无法获取列值。这是我的代码...
DataGridRow BillRow = sender as DataGridRow;
I get the selected row details into BillRow (I'm able to see them in Visualiser) but unable to get the values into a variable. Can you help me ??
我将选定的行详细信息放入 BillRow(我可以在 Visualiser 中看到它们)但无法将值放入变量中。你能帮助我吗 ??
采纳答案by Smaug
The following solution may be help you
以下解决方案可能对您有所帮助
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;
}

