vb.net 如何以编程方式使用 BindingNavigator?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20119220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:54:46  来源:igfitidea点击:

How to use BindingNavigator programmatically?

vb.net

提问by Thomas Carlton

I have a VB.net form and I'm trying to bind some data to controls.

我有一个 VB.net 表单,我正在尝试将一些数据绑定到控件。

I'm using the following code:

我正在使用以下代码:

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

The code works but when I move to the second row in the binding navigator, the controls don't update (see Video)

代码有效,但是当我移动到绑定导航器中的第二行时,控件不会更新(参见视频

What is missing to the code please ?

请问代码中缺少什么?

Thanks

谢谢

采纳答案by Thomas Carlton

Solution:

解决方案:

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