【发布时间】: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