我想刷新 DataGrid 中的数据,但不改变 DataGrid 中当前选中的行,所以我这样操作:
int selectedIndex = dataGrid1.SelectedIndex;
dataGrid1.ItemsSource = list;
dataGrid1.SelectedIndex = selectedIndex;
也就是说,我在 DataGrid 邦定前,先保存当前的 SelectedIndex,然后绑定后再设置 SelectedIndex,但这样运行时会报错:“对 COM 组件的调用返回了错误 HRESULT E_FAIL。”
于是我注释了“dataGrid1.SelectedIndex = selectedIndex;”,运行正常,
这可能是因为 程序在执行“dataGrid1.SelectedIndex = selectedIndex;”的时候,DataGrid的邦定工作并没有完成导致的,那我该怎么办才能实现我想要的结果?