根据 DataGridView 的行索引更改绑定列表项

change Binding List item based on the DataGridView's row index

我已经将 BindingList 绑定到 DataGridView。我现在要做的是更改 DataGridView 行中表示的 BindingList 的基础项。

所以我想要实现的是通过相应的 DataGridView 行索引找到 BindingList 的列表项,以便更改 BindingList 中的特定项目并最终更改相应的DataGridView 行。

您可以使用 DataGridViewRow.DataBoundItem Property 获取对绑定到特定 DataGridViewRow 的源对象的引用。由于您已将 BindingList 绑定到 DataGridView,因此返回的对象将源自该列表。像这样

DataGridViewRow row = ...;
var sourceObject = (YourObjectType)row.DataBoundItem;
// do something with the object

另一种方法是将 DataGridViewRow.Index Property 与您的绑定列表结合使用,如下所示:

DataGridViewRow row = ...;
var sourceObject = yourBindingList[row.Index];
// do something with the object