【问题标题】:Edit collection of BindingSource编辑 BindingSource 的集合
【发布时间】:2013-09-19 14:11:54
【问题描述】:

我有这种情况:

我有一些对象(例如人员列表)的列表或数组(尚未确定哪个更适合我的目的,但这没关系)。在我的表单中有一个 DataGridView,我想在其中查看所有人及其属性。可以在运行时编辑人员的属性,我想在 DataGridView 中立即看到这些更改。我为此使用了BindingSource

Dim _persons As New List(Of Person)
Dim persons As BindingSource = New BindingSource()

persons.DataSource = _persons
myGridView.DataSource = persons

现在,当我通过 BindingSource (persons) 添加/删除一个人时,这非常有效。我可以立即看到这种变化。但是如果我想编辑一个人怎么办?让我们来上课:

Public Class Person

     Public Property FirstName As Integer
     Public Property SecondName As String
     Public Property Address As String

End Class

如果我想编辑名字,我可以这样做:

 _persons(1).FirstName = "John"

但这是直接通过列表而不是通过 BindingSource,因此此更改不会在 DataGridView 中生效。有没有办法通过 BindingSource 来影响 DataGridView 中的此编辑?

很抱歉,这是我第一次使用 BindingSource,所以这可能是一个愚蠢的问题。谢谢各位。

【问题讨论】:

    标签: vb.net collections datagridview datasource bindingsource


    【解决方案1】:

    你应该直接影响BindingSource(不是你用来创建BindingSource的List),即:

    DirectCast(persons(1), Person).FirstName = "John"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多