选择数据网格(wpf)中的行索引
如何选择数据网格中的行索引?
事件 SelectionChanged
以下代码不起作用:
private DataGridRow dgr = new DataGridRow();
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.dgr = this.dataGrid.ItemContainerGenerator.ContainerFromItem(this.dataGrid.SelectedItem) as DataGridRow;
MessageBox.Show(this.dgr.GetIndex().ToString());
}
how Select the row index in datagrid ?
The event SelectionChanged
The following code does not work :
private DataGridRow dgr = new DataGridRow();
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.dgr = this.dataGrid.ItemContainerGenerator.ContainerFromItem(this.dataGrid.SelectedItem) as DataGridRow;
MessageBox.Show(this.dgr.GetIndex().ToString());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上面的代码不起作用的原因是因为 wpf 数据网格是虚拟化的,它可能不会使用 itemContainerGenerator.ContainerFromItem 返回行,因为它可能位于滚动视图之外。
为此,您必须使用数据网格的项目集合和使用所选项目的 IndexOf 调用。
The reason why above code would not work is because wpf data grid is virtualized and it may not return the row using the itemContainerGenerator.ContainerFromItem because it may be lying outside the scroll view.
For this you will have to use the datagrid's items collection and the IndexOf call using selected item.
我的回答迟了,但我希望它对从搜索引擎找到这篇文章的人仍然有用。这是更通用的解决方案,它还有助于定义所有选定行的索引。
现在 RowIndexes 包含所选行的所有索引。只需将代码放入您想要的事件中即可。
My answer is late, but I hope it still will be useful for people who found this post from search engines. This is more general solution which also helps to define indexes of all the selected rows.
And now RowIndexes contains all indexes of the selected rows. Just put the code inside the event you wish, that's all.
这是一个迟到的答案,但这就是我完成它的方式。这将为您提供 DataGrid 中每个选定行的索引(dgQuery 是我的 DataGrid 的名称):
它在索引 0 处给出 1,因此我们需要在每个索引处减去 1。
.Row[0]
实际上是 DataRowView 的一列(在我的脑海中)...,不知道为什么它被称为行。您可以将其更改为 [1]、[2] 等以查看该行中的其他单元格。使用此解决方案,您不需要集合、数组等任何东西。您只需处理手头的内容并利用现有代码即可。
至少对我来说,这个实现的巨大好处是它按照选择的顺序遍历选定的项目。如果您想知道用户选择的顺序,这可能是一个非常强大的工具。
我发布此内容是因为我刚刚花了 4 个多小时寻找解决方案。我什至放弃了复选框,因为我没有足够的时间来实现它们以使其正常工作......也许在未来。
This is a late answer, but this is how I accomplished it. This gives you index of every selected row in the DataGrid (dgQuery is the name of my DataGrid):
It gives 1 at index 0, so we need to subtract 1 at every index.
.Row[0]
Is actually a column (in my head)... of that DataRowView, not sure why it's called a row. You can change it to [1], [2] and so on to view other cells within that row.With this solution, you don't need a collection, array, nothing of that sort. You just work with what's at hand and make use of existing code.
The huge plus side of this implementation, at least for me, was the fact that it goes through selected items in the order they were selected. This can be a very powerful tool if you wish to know the order of user's selection.
I'm posting this because I just spent over 4 hours looking for a solution. I even gave up on check boxes because I don't have enough time to implement those to work well... maybe down the road.