简体   繁体   English

在datagrid(wpf)中选择行索引

[英]Select the row index in datagrid (wpf)

how Select the row index in datagrid ? 如何在datagrid中选择行索引?

The event SelectionChanged 事件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());

    }

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. 上面的代码不起作用的原因是因为wpf数据网格已虚拟化,并且可能无法使用itemContainerGenerator.ContainerFromItem返回该行,因为它可能位于滚动视图之外。

For this you will have to use the datagrid's items collection and the IndexOf call using selected item. 为此,您将必须使用datagrid的items集合和使用选定项目的IndexOf调用。

   private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
         var dg = sender as DataGrid;
         MessageBox.Show(dg.Items.IndexOf(dg.SelectedItem).ToString());
   }

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. 这是更通用的解决方案,也有助于定义所有选定行的索引。

List<int> RowIndexes = new List<int>();
int SelectedItemsCount = yourDataGrid.SelectedItems.Count;
   for (int i = 0; i < SelectedItemsCount ; i++)
            {
               RowIndexes.Add(yourDataGrid.Items.IndexOf(yourDataGrid.SelectedItems[i]));
            }

And now RowIndexes contains all indexes of the selected rows. 现在,RowIndexes包含所选行的所有索引。 Just put the code inside the event you wish, that's all. 只需将代码放入您想要的事件中,仅此而已。

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): 这为您提供了DataGrid中每个选定行的索引(dgQuery是我的DataGrid的名称):

foreach (var selection in dgQuery.SelectedItems)
{
       DataRowView row = (DataRowView)item;

       int index = Convert.ToInt32(row.Row[0]) - 1;
}

It gives 1 at index 0, so we need to subtract 1 at every index. 它在索引0处给出1,因此我们需要在每个索引处减去1。

.Row[0] Is actually a column (in my head)... of that DataRowView, not sure why it's called a row. .Row[0]实际上是该DataRowView的一列(在我的脑海中)...,不确定为什么将其称为行。 You can change it to [1], [2] and so on to view other cells within that row. 您可以将其更改为[1],[2]等,以查看该行中的其他单元格。

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. 我发布此消息是因为我只花了4个多小时来寻找解决方案。 I even gave up on check boxes because I don't have enough time to implement those to work well... maybe down the road. 我什至放弃了复选框,因为我没有足够的时间来实现这些复选框以使其正常工作……也许是在将来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM