我有一个VB.net表单,我正在尝试将一些数据绑定到控件。
我正在使用以下代码:
Private Sub InitDataLayer()
'Create table
DataTable = New DataTable
DataTable.Columns.Add("ID")
DataTable.Columns("ID").DataType = GetType(Integer)
DataTable.Columns("ID").AllowDBNull = False
DataTable.Columns.Add("Name")
DataTable.Columns("Name").DataType = GetType(String)
DataTable.Columns("Name").AllowDBNull = False
'Create new rows
'1st Row
Dim NewRow As DataRow = DataTable.NewRow
NewRow.Item("ID") = 1
NewRow.Item("Name") = "John"
DataTable.Rows.Add(NewRow)
'2nd Row
NewRow = DataTable.NewRow
NewRow.Item("ID") = 2
NewRow.Item("Name") = "Steve"
DataTable.Rows.Add(NewRow)
'Bind controls
'Textboxes
TextBoxID.DataBindings.Add(New Binding("text", DataTable, "ID"))
TextBoxName.DataBindings.Add(New Binding("text", DataTable, "Name"))
'Navigator
Dim BS As New BindingSource
BS.DataSource = DataTable
BindingNavigator1.BindingSource = BS
End Sub
代码有效,但当我移动到绑定导航器中的第二行时,控件不会更新(请参阅Video)
请问代码缺少什么?
由于
答案 0 :(得分:0)
解决方案:
Private Sub InitDataLayer()
'Create table
DataTable = New DataTable
DataTable.Columns.Add("ID")
DataTable.Columns("ID").DataType = GetType(Integer)
DataTable.Columns("ID").AllowDBNull = False
DataTable.Columns.Add("Name")
DataTable.Columns("Name").DataType = GetType(String)
DataTable.Columns("Name").AllowDBNull = False
'Create new rows
Dim NewRow As DataRow = DataTable.NewRow
NewRow.Item("ID") = 1
NewRow.Item("Name") = "John"
DataTable.Rows.Add(NewRow)
NewRow = DataTable.NewRow
NewRow.Item("ID") = 2
NewRow.Item("Name") = "Steve"
DataTable.Rows.Add(NewRow)
'Bind controls
Dim BS As New BindingSource
BS.DataSource = DataTable
TextBoxID.DataBindings.Add(New Binding("text", BS, "ID"))
TextBoxName.DataBindings.Add(New Binding("text", BS, "Name"))
BindingNavigator1.BindingSource = BS
End Sub