使用datagridview添加记录

时间:2016-01-07 23:56:10

标签: vb.net winforms datagridview bindingsource

我正在使用VS2008 / .net fw 3.5sp1并重新进入编码生活 - 至少说一点生锈:)下面的任何帮助都会很棒。

我需要从用户那里收集一个输入列表,稍后我将使用它(传递给带有其他值的teradata DWH)。输入列表包括两部分,BSB ID和帐户ID。经过一些研究,看起来最好的选择是为帐户创建一个类,一个帐户列表并将其绑定到datagridview - 我已经完成了 - 但看起来我无法添加/编辑。我添加了一个新的按钮/添加按钮来更改数据网格并得到一个我无法以编程方式添加的错误。

当我使用accountList.AllowNew()= TRUE时 - 错误 - 未找到类型bankaccount的构造函数 - 但是 - 我认为构造函数是类中的“新”子?

当我尝试使用accountsBindingSource.IsFixedSize = False时,它会告知该属性是只读的。

为此 - 我已将所有其他代码剪切到此部分,这需要一个表单(frmAccountLoad),datagridview dgvAccounts和一个按钮btnNewLine。

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
Imports System.Diagnostics
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Windows.Forms
'--------------------------------------------------------------------
Public Class frmAccountLoad
    ' This BindingSource binds the list to the DataGridView control. 
    Private accountsBindingSource As New BindingSource()

    Private Sub frmAccountLoad_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'Create list to hold accounts
        Dim accountList As New BindingList(Of BankAccount)
        accountList.AllowNew() = True
        'accountList.AllowEdit = True


        accountsBindingSource.DataSource = accountList


        dgvAccounts.DataSource = accountsBindingSource

        'dgvAccounts.Columns(0).HeaderText = "BSB"
        'dgvAccounts.Columns(1).HeaderText = "Account"

    End Sub
    Private Sub btnNewLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewLine.Click
        'accountsBindingSource.IsFixedSize = False

        accountsBindingSource.AddNew()
    End Sub

End Class
'--------------------------------------------------------------------
Public Class BankAccount
    '----------------------------------------------------------
    'a bank account has both a BSB and an account number
    Private m_BSB As String
    Private m_Account As String

    '----------------------------------------------------------
    'Public Property
    Public Property BSB() As String
        Get
            Return m_BSB
        End Get
        Set(ByVal value As String)
            m_BSB = value
        End Set
    End Property
    Public Property Account() As String
        Get
            Return m_Account
        End Get
        Set(ByVal value As String)
            m_Account = value
        End Set
    End Property
    '----------------------------------------------------------
    Public Sub New(ByVal new_Bsb As String, ByVal new_Account As String)
        m_BSB = new_Bsb
        m_Account = new_Account
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

为了能够在AddNew BindingList上致电BankAccountPublic Class BankAccount Public Property BSB As String Public Property Account As String Public Sub New() 'Do initialization here if you need 'Or Remove the constructor if you don't need any initialization. End Sub End Class 应该有一个无参数构造函数。

如果你需要一些初始化,你需要一个没有公共参数的构造函数,或者如果你不需要初始化就删除任何构造函数,那么将使用默认的无参数构造函数。

accountList.AllowNew = True

此外,您无需设置DataSource。使用BindingList(Of T)作为Private accountList As BindingList(Of BankAccount) Private Sub frmAccountLoad_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load accountList = New BindingList(Of BankAccount) dgvAccounts.DataSource = BS End Sub 就足够了。

accountList.AddNew()

然后,您可以在任何需要的地方拨打lmap i $args { format {$%s} $i }