C# WPF 工具包 DataGrid SelectionChanged 获取单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2148978/
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
WPF Toolkit DataGrid SelectionChanged Getting Cell Value
提问by Dan Bater
Please help me, Im trying to get the value of Cell[0] from the selected row in a SelectionChangedEvent.
请帮助我,我试图从 SelectionChangedEvent 中的选定行获取 Cell[0] 的值。
I am only managing to get lots of different Microsoft.Windows.Controls and am hoping im missing something daft.
我只是设法获得了许多不同的 Microsoft.Windows.Controls,我希望我错过了一些愚蠢的东西。
Hoping I can get some help from here...
希望我能从这里得到一些帮助......
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Microsoft.Windows.Controls.DataGrid _DataGrid = sender as Microsoft.Windows.Controls.DataGrid;
}
I was hoping it would be something like...
我希望它会像......
_DataGrid.SelectedCells[0].Value;
However .Value isn't an option....
但是 .Value 不是一个选项......
Many many thanks this has been driving me mad! Dan
非常感谢这让我发疯!担
采纳答案by serge_gubenko
pls, check if code below would work for you:
请检查以下代码是否适合您:
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
if (e.AddedItems!=null && e.AddedItems.Count>0)
{
// find row for the first selected item
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
// find grid cell object for the cell with index 0
DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
if (cell != null)
{
Console.WriteLine(((TextBlock)cell.Content).Text);
}
}
}
}
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null) child = GetVisualChild<T>(v);
if (child != null) break;
}
return child;
}
hope this helps, regards
希望这有帮助,问候
回答by Anshuman Sharma
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid _DataGrid = sender as DataGrid;
string strEID = _DataGrid.SelectedCells[0].Item.ToString();
}
回答by Hemendra Singh
This will give you the current selected row in the DataGrid in WPF:-
这将为您提供 WPF 中 DataGrid 中当前选定的行:-
DataRow dtr = ((System.Data.DataRowView)(DataGrid1.SelectedValue)).Row;
Now to get the cell value just write dtr[0]
, dtr["ID"]
, etc.
现在,让单元格的值只写dtr[0]
,dtr["ID"]
等等。
回答by Ayaz Alifov
Less code, and it works.
更少的代码,它的工作原理。
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
DataGridCell RowColumn = dataGrid.Columns[ColumnIndex].GetCellContent(row).Parent as DataGridCell;
string CellValue = ((TextBlock)RowColumn.Content).Text;
}
ColumnIndex is the index of the column you want to know.
ColumnIndex 是您想知道的列的索引。
回答by Exel Gamboa
Since you are using the "SelectionChanged", you can use the sender as a Data Grid:
由于您使用的是“SelectionChanged”,您可以将发件人用作数据网格:
DataGrid dataGrid = sender as DataGrid;
DataRowView rowView = dataGrid.SelectedItem as DataRowView;
string myCellValue = rowView.Row[0].ToString(); /* 1st Column on selected Row */
I tried the answers posted here and were good, but gave me problems when started to hide columns in the DataGrid. This one works for me even when hiding columns. Hope it works for you too.
我尝试了这里发布的答案并且很好,但是在开始隐藏 DataGrid 中的列时给我带来了问题。即使在隐藏列时,这个也对我有用。希望它也适用于你。