使用 Datagrid 的 SelectedItem 属性更新 BindingSource 的 Current 属性?

发布于 2024-11-04 00:41:30 字数 291 浏览 13 评论 0原文

有没有办法将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在夏天 2024-11-11 00:41:30

当您单击 DataGridView 中的特定行时,将 BindingSource 对象的 Position 属性设置为适当的值,而不是为其分配选定的行DataGridView

When you click on a specific row in the DataGridView set the Position property of the BindingSource object to the appropriate value instead of assigning a selected row for the DataGridView.

-小熊_ 2024-11-11 00:41:30

如果您希望数据网格和集合同步,则必须为数据网格设置 IsSynchronizedWithCurrentItem = true 。您的集合应该是 ICollectionView 或 BindingListCollectionView 类型。您可以通过以下方式实现此目的:

this.view = CollectionViewSource.GetDefaultView(this.mysourcecollection)

如果您这样做,您所选择的项目将始终保持同步。您可以使用 ICollectionView 的 MoveTo 方法来逐步浏览您的项目。

如果您还有其他问题,请留下一些评论

编辑:

如果您使用数据表作为集合源,只需创建 BindingListCollectionView 类型的属性。

public BindingListCollectionView MyView
{ get;set;}

初始化数据表后,以这种方式初始化 BindingListCollectionView:

 this.MyView = (BindingListCollectionView )CollectionViewSource.GetDefaultView(this.mydatatable);

然后只需使用此视图作为数据网格的 ItemsSource,

<DataGrid ItemsSource="{Binding MyView}" IsSynchronizedWithCurrentItem="true" />

您现在可以做的是调用:

this.MyView.MoveCurrentToFirst();
this.MyView.MoveCurrentToLast();
this.MyView.MoveCurrentToNext();
this.MyView.MoveCurrentToPrevious();
//and some other

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

this.view = CollectionViewSource.GetDefaultView(this.mysourcecollection)

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.

public BindingListCollectionView MyView
{ get;set;}

after initialize your datatable init the BindingListCollectionView in this way:

 this.MyView = (BindingListCollectionView )CollectionViewSource.GetDefaultView(this.mydatatable);

then just use this view as ItemsSource for your datagrid

<DataGrid ItemsSource="{Binding MyView}" IsSynchronizedWithCurrentItem="true" />

what you can do now is to call:

this.MyView.MoveCurrentToFirst();
this.MyView.MoveCurrentToLast();
this.MyView.MoveCurrentToNext();
this.MyView.MoveCurrentToPrevious();
//and some other
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文