Access bindingsource column value

夙愿已清 提交于 2020-01-04 09:13:57

问题


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:


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



来源:https://stackoverflow.com/questions/3169730/access-bindingsource-column-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!