VGD
VGD

Reputation: 466

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

I have binded a BindingList to a DataGridView. What I'm trying to do now is changing the underlying item of the BindingList represented in a DataGridView row.

So what I'm trying to achieve is to find a list item of the BindingList by the corresponding DataGridView row index in order to change that specific item in the BindingList and ultimately the corresponding DataGridView row.

Upvotes: 1

Views: 1836

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205559

You can use DataGridViewRow.DataBoundItem Property to get reference to the source object bound to a specific DataGridViewRow. Since you have binded the BindingList to the DataGridView, the object returned will originate from that list. Something like this

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

Another way is to use DataGridViewRow.Index Property in combination with your binding list like this:

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

Upvotes: 1

Related Questions