访问绑定源列值

发布于 2024-09-07 16:38:03 字数 237 浏览 7 评论 0原文

如何使用代码更新绑定源中的列值?

我正在尝试类似的操作:

CustomersBindingSource.AddNew();
CustomersBindingSource.Current["CustomerID"] = Guid.NewGuid();

此代码当前错误指出:“无法将 [] 索引应用到‘object’类型的表达式”。

非常感谢任何重写此内容的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-09-14 16:38:03

BindingSource 的 Current 属性在以下环境中非常通用它返回什么:类型对象。对象没有定义索引器,因此您的 [] 不起作用。您需要做的是将 Current 属性转换为它真正的(更具体的)类型。

例如,如果 Current 实际上是 DataRowView,您可以编写:

DataRowView current = (DataRowView)CustomersBindingSource.Current;
current["CustomerID"] = Guid.NewGuid();    

希望这有帮助,

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文