数据网格视图中的选定索引

本文关键字:索引 数据网 网格 视图 数据 | 更新日期: 2023-09-27 18:20:51

我有两个表。我从表1中选择一条记录,在其中一个信息窗口中双击即可显示表2中的信息。关闭此窗口后,我必须刷新这两个表,并希望在表1中选中同一行。我在wpf工作,我使用selectedindex。

现在我暂时使用这个:

int index = dgTable1.Grid.CurrentRow.Index;
///**
frm.ShowDialog();
frm.Dispose();
ReloadTable1();
selectedindex(index)

带有

private void selectindex(int index)
{
    dgwTable1.Grid.Rows[index].Selected = true;
    DATAtype data= dgwTable1.GetObjectFromRow<DATAtype>(index);
    LoadTable2(data);
}

它可以工作,但我有表1的Grid_SelectionChanged,不需要启动,我必须重新加载数据,如果我使用它时滚动条向下,我会返回到表1的顶部!

但我知道这不是正确的方法:(而且在wpf:/中太简单了

dgTable是一个UserControlDataGridView作为网格

数据网格视图中的选定索引

保存当前索引:

 int index = dataGridView1.CurrentRow.Index;

编辑后:

 dataGridView1.Rows[index].Selected = true; 

希望这能有所帮助。

是否可以是DataViewBindingSource,并更好地使用过滤器?

DataView view = new DataView(_table);
BindingSource tSource = new BindingSource();
tSource.DataSource = view;
_dataGridView.DataSource = _tSource;
_tSource.Filter = "Value=0";

像这样。。。

要保存滚动,请使用myDataGridView.FirstDisplayedScrollingRowIndex

int scrollIndex = 0;
        if (myDataGridView.FirstDisplayedScrollingRowIndex >= 0)
            scrollIndex = myDataGridView.FirstDisplayedScrollingRowIndex;

编辑后:

if (myDataGridView.Rows.Count > 0)
                    myDataGridView.FirstDisplayedScrollingRowIndex = scrollIndex;

希望这能有所帮助。

在我看来,解决问题的最简单方法是使用MVVM模式。在包含两个DataGrids的视图的ViewModel中,您有一个类似"SelectedNameOfContentClass"的属性,该属性绑定到第一个DataGridsSelectedValue属性。此"SelectedNameOfContentClass"属性也绑定到秒数据网格DataSource。因此,如果您更改第一个DataGrid中的选定行,第二个DataGrid的Source会自动更新。

绑定到第一个Datagrids DataSource的列表应该是ObservableCollection。对于对话框,您可以在对象中使用IEditableObject接口。