使用 Datagrid 的 SelectedItem 属性更新 BindingSource 的 Current 属性?
有没有办法将 BindingSource 的当前项目属性与 Datagrid 的选定项目同步?例如,我单击 Datagrid 中的第 10 行,按下按钮调用 BindingSource.MoveNext(),然后使用 BindingSource.Current 设置 Datagrid.SelectedItem;突出显示的行是第 2 行而不是第 11 行。
我这样做的方式是否错误?我只是想让 datagrid 在操作后突出显示下一行(例如更新我单击的行),但 Datagrid 似乎没有提供执行此操作的方法,而我只能处理 BindingSource。
Is there a way to synchronize a BindingSource's current item property with the Datagrid's selected item? For example, I click on row 10 in the Datagrid, when I press a button to invoke BindingSource.MoveNext() then setting the Datagrid.SelectedItem with BindingSource.Current; the row highlighted is row 2 instead of 11.
Am I doing this the wrong way? I simply want to datagrid to highlight the next row after an operation (such as updating the row I clicked on), but Datagrid does not seem to offer a way to do that, and I'm left with dealing with BindingSource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您单击
DataGridView
中的特定行时,将BindingSource
对象的Position
属性设置为适当的值,而不是为其分配选定的行DataGridView
。When you click on a specific row in the
DataGridView
set thePosition
property of theBindingSource
object to the appropriate value instead of assigning a selected row for theDataGridView
.如果您希望数据网格和集合同步,则必须为数据网格设置 IsSynchronizedWithCurrentItem = true 。您的集合应该是 ICollectionView 或 BindingListCollectionView 类型。您可以通过以下方式实现此目的:
如果您这样做,您所选择的项目将始终保持同步。您可以使用 ICollectionView 的 MoveTo 方法来逐步浏览您的项目。
如果您还有其他问题,请留下一些评论
编辑:
如果您使用数据表作为集合源,只需创建 BindingListCollectionView 类型的属性。
初始化数据表后,以这种方式初始化 BindingListCollectionView:
然后只需使用此视图作为数据网格的 ItemsSource,
您现在可以做的是调用:
if you want your datagrid and your collection in sync you have to set IsSynchronizedWithCurrentItem = true for your datagrid. your collection should be of type ICollectionView or BindingListCollectionView. you can achive this with
if you do this your selecteditems is always in sync. And you can use the MoveTo -methods of your ICollectionView to step through your items.
if you have further questions, just leave some comments
EDIT:
if you use datatables as collectionsource just create a property of type BindingListCollectionView.
after initialize your datatable init the BindingListCollectionView in this way:
then just use this view as ItemsSource for your datagrid
what you can do now is to call: