none
如何用代码选中一个DataGrid的一行 RRS feed

  • 问题

  • 大家好,我自定义了一个类(GreeningPointDataModel)然后将一个List<GreeningPointDataModel>绑定到一个DataGrid,代码如下

    private void UpdateDataGridPointList(List<GreeningPointDataModel> modelList)
            {
                txtPointListCount.Text = modelList.Count.ToString();
                PagedCollectionView pagedCollectionView = new PagedCollectionView(modelList);
                pagedCollectionView.PageSize = 10;
                dataPagePointList.Source = pagedCollectionView;
                dataGridPointList.ItemsSource = pagedCollectionView;
            }

    然后我在代码中获得了一个GreeningPointDataModel的实例,想通过这个实例将DataGrid上对应的一行设置为选中状态。这个如何实现?GreeningPointDataModel类中有ID选项,是唯一的。就想通过这个ID实现。

    我自己写了个方法,但并不能正常工作

    private void SetDataGridSelectedRow(DataGrid dataGrid, GreeningPointDataModel model)
            {
                int index = -1;
                foreach (var p in dataGrid.ItemsSource) {
                    var point = p as GreeningPointDataModel;
                    if (point == null) continue;
                    if (point.Id == model.Id) {
                        index = dataGrid.SelectedItems.IndexOf(p);//这一行可以被执行到,但返回值仍然是-1
                        break; ;
                    }
                }
                if (index != -1)
                    dataGrid.SelectedIndex = index;
            }
    index的值仍然是-1



    2017年6月25日 8:33

全部回复


  • Hi waterharbin,

    根据你的描述,我写了下面的代码来选择DataGrid的行,你可以参考。

            <sdk:DataGrid x:Name="TESTDG" HorizontalAlignment="Left" Height="145" Margin="30,320,0,0" VerticalAlignment="Top" Width="320"/>
            <Button x:Name="button1" Content="Set row" HorizontalAlignment="Left" Margin="370,390,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                SetDataGridSelectedRow(TESTDG , listauthors[2]); ;
            }
    
            private void SetDataGridSelectedRow(DataGrid dataGrid, Author model)
            {
                int index = -1;
                foreach (var p in dataGrid.ItemsSource)
                {
                    Author point = p as Author;
                    if (point == null) continue;
                    if (point.ID == model.ID)
                    {
                        List<Author> list = dataGrid.ItemsSource as List<Author>;
                        index = list.IndexOf(point);
                        break; ;
                    }
                }
                if (index != -1)
                    dataGrid.SelectedIndex = index;
            }
    
    

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年7月3日 6:16