使用数据网格视图添加记录

Using a datagridview to add records

我正在使用 VS2008/.net fw 3.5sp1 并回到编码生活 - 至少可以说有点生疏:) 请在下面提供任何帮助。

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

当我使用 accountList.AllowNew() = TRUE 时——错误——找不到类型 bankaccount 的构造函数——但是——我认为构造函数是 class 中的 "new" 子?

当我尝试 accountsBindingSource.IsFixedSize = False 时,它​​提示 属性 是只读的。

为此 - 我已将所有其他代码剪切到此部分,它需要一个表单 (frmAccountLoad),带有一个数据网格视图 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

为了能够在您的 BindingList 上调用 AddNewBankAccount 应该有一个 parameter-less 构造函数。

如果你需要一些初始化,你需要有一个 public parameter-less 构造函数,或者如果你不需要初始化,只需删除任何构造函数,那么默认的 parameter-less 构造函数将被使用。

Public 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。将 BindingList(Of T) 用作 DataSource.

就足够了
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()