Vb.net 将数据集绑定到循环中动态生成的文本框

Vb.net 将数据集绑定到循环中动态生成的文本框,vb.net,textbox,dataset,Vb.net,Textbox,Dataset,在将动态生成的文本框绑定到数据集时,我无法使其显示正确的数据 数据集包含2个表。Environments表有4行,其值分别为DEV、TST、ACC、PROD Dim cma As CurrencyManager cma = CType(BindingContext(oConfData.dsJimConfig.Tables("Environments")), CurrencyManager) For i As Integer = 0 To intNrEnvCombi - 1 cma.Pos

在将动态生成的文本框绑定到数据集时,我无法使其显示正确的数据

数据集包含2个表。
Environments
表有4行,其值分别为
DEV
TST
ACC
PROD

Dim cma As CurrencyManager
cma = CType(BindingContext(oConfData.dsJimConfig.Tables("Environments")), CurrencyManager)

For i As Integer = 0 To intNrEnvCombi - 1
   cma.Position = i

   Dim txt As TextBox = New TextBox
   With txt
      .Margin = New Padding(5)
      .TabIndex = intTabIdx
      .Name = arrLabelText(3) & i
         If i > cma.Count - 1 Then
          .Text = ""
        Else
          .DataBindings.Add(New Binding("Text", oConfData.dsJimConfig.Tables("Environments"), "Environment"))
        End If
      .CausesValidation = True
    AddHandler txt.Validating, AddressOf Text_Validating
    AddHandler txt.GotFocus, AddressOf Text_setClear
    AddHandler txt.Leave, AddressOf Text_Format
  End With
Next
在所有文本框中,显示最后一条记录的数据
PROD


我在这里做错了什么?

如果要将
控件
绑定到数据源的特定元素(而不是整个集合),可以使用并将其设置为要绑定到的元素的索引


考虑这个简单的例子:

Dim table = New DataTable()
table.Columns.Add("FooBar")
table.Rows.Add({"First"})
table.Rows.Add({"Second"})
table.Rows.Add({"Third"})

Dim panel = new FlowLayoutPanel()
For i = 0 To 2 
    Dim bs = new BindingSource With
    {
        .DataSource = table,
        .Position = i
    }

    Dim box = new TextBox
    box.DataBindings.Add(New Binding("Text", bs, "FooBar"))
    panel.Controls.Add(box)
Next

Dim f = new Form()
panel.Dock = DockStyle.Fill
f.Controls.Add(panel)
f.ShowDialog()
结果:


太多了!已更改用于更新DS的数据绑定。添加(新绑定(“Text”,bsEnv,“Environment”,True,DataSourceUpdateMode.OnValidation))