将行添加到类绑定的datagridview [英] Add rows to class bound datagridview

查看:115
本文介绍了将行添加到类绑定的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绑定对象的列表到datagridview
绑定现有的列表(Of是工作,但我想添加或删除对象,我的dgv不更新。 / p>

 公共类Form1 
Dim lt作为新列表(测试)
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load
lt.Add(New Test(Mac,2200))
lt.Add(New Test(PC,1100) )
dgv.DataSource = lt

lt.Add(新测试(Android,3300))'这一行不会出现在dgv
End Sub
结束类

公共类测试
Public Sub New(ByVal name As String,ByVal cost As String)
_name = name
_cost = cost
End Sub
私有_name作为字符串
公共属性名称()As String
获取
返回_name
结束Get
设置(ByVal value As String)
_name = value
结束集
结束属性
私有_cost As String
公共属性成本()As String
获取
返回_cost
End
设置(ByVal value As String)
_cost = value
结束设置
结束属性
结束类

如何添加,删除或更改值从dgv到列表和逆?



NoiseBe

解决方案

更改此行:

  Dim lt作为新列表(测试)

To:

 导入System.ComponentModel 
...
私人lt作为新的BindingList(测试)

当集合内容将更改时,应使用 BindingList(Of T)



如果除了列表内容,列表 将会改变(如Test.Name),您也应该在类本身上实现 INotifyPropertyChanged






另一种方法:

  dgv.DataSource =没有
lt.Add(新测试(Android,3300))
dgv.DataSource = lt

这会重置 DataSource ,以便显示新内容。然而,它意味着其他几件事情被重置像选定的项目;如果您绑定到List / Combo控件,您还必须重置 ValueMember DisplayMember 属性。


Hi I'm trying to bind a list of objects to a datagridview Binding a existing list(Of is working but I'm trying to add or remove a object from, my dgv isn't updating.

    Public Class Form1
    Dim lt As New List(Of Test)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lt.Add(New Test("Mac", 2200))
        lt.Add(New Test("PC", 1100))
        dgv.DataSource = lt

        lt.Add(New Test("Android", 3300)) 'This line won't appear in the dgv
    End Sub
End Class

Public Class Test
    Public Sub New(ByVal name As String, ByVal cost As String)
        _name = name
        _cost = cost
    End Sub
    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Private _cost As String
    Public Property Cost() As String
        Get
            Return _cost
        End Get
        Set(ByVal value As String)
            _cost = value
        End Set
    End Property
End Class

How can I add or remove or change a value from the dgv to the list and inverse?

NoiseBe

解决方案

Change this line:

Dim lt As New List(Of Test)

To:

Imports System.ComponentModel
...
Private lt As New BindingList(Of Test)

When the collection contents will change, you should use a BindingList(Of T). This collection has events associated with it which make it aware of changes to the list contents.

If in addition to the list contents, the list items will change (like Test.Name), you should also implement INotifyPropertyChanged on the class itself.


Another way to do it:

dgv.DataSource = Nothing
lt.Add(New Test("Android", 3300))
dgv.DataSource = lt

This "resets" the DataSource so that the new contents will show up. However, it means that several other things get reset like selected items; if you are binding to a List/Combo control, you will also have to reset the ValueMember and DisplayMember properties as well.

这篇关于将行添加到类绑定的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆