【问题标题】:Using a datagridview to add records使用 datagridview 添加记录
【发布时间】:2016-01-07 23:56:09
【问题描述】:

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

我需要收集来自用户的输入列表,稍后我将使用该列表(传递给具有其他一些值的 teradata DWH)。输入列表包括两个部分,一个 BSB ID 和一个 Account 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

【问题讨论】:

  • BankAccount 应该有一个无参数构造函数。

标签: vb.net winforms datagridview bindingsource


【解决方案1】:

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

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

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()

【讨论】:

  • 谢谢,我已经删除了参数,并允许new..当我运行代码时,单击添加新行按钮,(获取新行以输入 BSB/帐户) - 我收到错误 - 无法将项目添加到只读或固定大小的列表中。
  • 您不需要任何其他无参数构造函数并将BindingList 设置为DataGridViewDataSource
  • 谢谢,我已经用你上面的代码更新了,但是当我运行 addnew() 时,我得到一个关于构造函数的错误 - 我将把错误添加到我上面的帖子中。
  • 查看编辑。您需要以这种方式拥有BankAccout 课程。如果您需要一些初始化,则需要有一个公共无参数构造函数,或者如果您不需要初始化,则只需删除任何构造函数,然后将使用默认构造函数。也像我一样使用自动属性。
猜你喜欢
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
相关资源
最近更新 更多