WPF-如何在数据网格中获取选定的行索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36518734/
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- How to get selected row index in datagrid?
提问by Santhosh
I have the textbox in datagrid. Data are taking from database. assume I have 10 rows with these textbox value. once I click on this row, able to get this selected row index. My goal is if once textbox value are get change, I need to detect which row it is (which value)and do some calculation based on this value then need to display another field of the same row. So I am in a position to know which row being get hit. `I am using Datagrid with following declarations:
我在数据网格中有文本框。数据取自数据库。假设我有 10 行这些文本框值。一旦我点击这一行,就能够获得这个选定的行索引。我的目标是,如果一旦文本框值发生变化,我需要检测它是哪一行(哪个值)并根据该值进行一些计算,然后需要显示同一行的另一个字段。所以我可以知道哪一行被击中。`我正在使用带有以下声明的 Datagrid:
<dg:DataGrid Name="dgBudgetAllocation" CanUserDeleteRows="False" CanUserAddRows="False" CanUserSortColumns="True"
IsSynchronizedWithCurrentItem="True" MaxHeight="400" RowHeight="70" SelectionUnit="Cell" SelectedValue="" SelectionMode="Single"
AutoGenerateColumns="False" GridLinesVisibility="None" HeadersVisibility="Column" PreviewMouseDown="DgBudgetAllocation_OnPreviewMouseDown" SelectedCellsChanged="DgBudgetAllocation_OnSelectedCellsChanged" MouseDown="DgBudgetAllocation_OnMouseDown" PreviewMouseUp="DgBudgetAllocation_OnPreviewMouseUp" PreviewKeyDown="DgBudgetAllocation_OnPreviewKeyDown" HorizontalAlignment="Left">
<dg:DataGridTemplateColumn Header="Budget Type" SortMemberPath="BUDGETYPE"
MinWidth="50" HeaderStyle="{DynamicResource dgHeaderLeftJust}" CellStyle="{DynamicResource dgColumnRightJust}">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding BUDGETYPE}" HorizontalAlignment="left" VerticalAlignment="Top" Margin="0,0,3,0" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
I have tried the following snippet based on various person suggestion. for all I am getting the selected index is -1.
我根据不同的人的建议尝试了以下代码段。对于所有我得到的选定索引是-1。
DataRowView drv = (DataRowView)dgBudgetAllocation.SelectedItem;
object item = dgBudgetAllocation.SelectedItem;
string ID = (dgBudgetAllocation.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
DataGrid row1 = (DataGrid)dgBudgetAllocation.SelectedItems[1];
var row = dgBudgetAllocation.SelectedItems[0];
Nothing is working. Please suggest me how to proceed further .
没有任何工作。请建议我如何进一步进行。
回答by Ilan
cSubscribe for the selection changing event( SelectionChanged="ItemsView_OnSelectionChanged") and use the handler to get all things you need. You can do that in behavior(and MVVM) or just put the handler inside your code behind.
c 订阅选择更改事件(SelectionChanged="ItemsView_OnSelectionChanged")并使用处理程序获取您需要的所有内容。你可以在行为(和 MVVM)中做到这一点,或者只是把处理程序放在你的代码后面。
A handler code example
处理程序代码示例
private void ItemsView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var dg = sender as DataGrid;
if (dg == null) return;
var index = dg.SelectedIndex;
//here we get the actual row at selected index
DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
//here we get the actual data item behind the selected row
var item = dg.ItemContainerGenerator.ItemFromContainer(row);
}
Let me know if you need more explanation. Regards.
如果您需要更多解释,请告诉我。问候。

