Access bindingsource column value

2019-07-16 08:15发布

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条回答
家丑人穷心不美
2楼-- · 2019-07-16 09:03

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

查看更多
登录 后发表回答