0% found this document useful (0 votes)
134 views1 page

Access Bindingsource Column Value

To update a column value in a BindingSource with code, you need to cast the Current property to the specific type it represents, such as a DataRowView. Then you can access the column value using indexing syntax on the cast object, like ((DataRowView)CustomersBindingSource.Current)["CustomerID"] = Guid.NewGuid();. The Current property itself returns a generic object that doesn't support indexing, so casting is required to access columns by name.

Uploaded by

Marcel Chis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views1 page

Access Bindingsource Column Value

To update a column value in a BindingSource with code, you need to cast the Current property to the specific type it represents, such as a DataRowView. Then you can access the column value using indexing syntax on the cast object, like ((DataRowView)CustomersBindingSource.Current)["CustomerID"] = Guid.NewGuid();. The Current property itself returns a generic object that doesn't support indexing, so casting is required to access columns by name.

Uploaded by

Marcel Chis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Access bindingsource column value

Access bindingsource column value


Question
How can I update a column value in a binding source with code?
I am trying for something similar to this:
CustomersBindingSource.AddNew();
CustomersBindingSource.Current["CustomerID"] = Guid.NewGuid();

This code currently errors stating: "Cannot apply indexing with [] to an expression of type
'object'".
Any help re-writing this is greatly appreciated!

1 Answer
BindingSource's Current property is very generic in what it returns: type object. Object
doesn't define an indexer so your [] doesn't work. What you need to do is cast the
Current property to the (more-specific) type of what it really is.
For example, if Current is really a DataRowView, you could write:
DataRowView current = (DataRowView)CustomersBindingSource.Current;
current["CustomerID"] = Guid.NewGuid();

Hope this helps,


Ben

How to select current column specific value from


BindingSource
label1.text = ((DataRowView)this.BindingSource1.Current).Row["PO"].ToString();

You might also like