在做系统时,我做了一个简单的窗体,具体如下图:
我想将combobox1与textbox1进行绑定.实现我在combobox中选择一个编号,对应的下面的textbox中就显示一个对应表中的姓名.而相关的记录都放于一张worker表中.
我利用读取器reader将数据读取到combobox中代码如下:
Dim i As Integer
Dim str As String
str = "select id from worker"
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
comm.Connection = conn
comm.CommandType = CommandType.Text
comm.CommandText = str
da.SelectCommand = comm
da.Fill(ds, "worker")
'ComboBox1.Items.Add(ds.Tables("worker").Columns(0).ColumnMapping)
reader1 = comm.ExecuteReader()
If reader1.HasRows Then
While reader1.Read
ComboBox1.Items.Add(reader1(0))
End While
End If
reader1.Close()
然而,怎样去绑定选择的记录的姓名呢?
我这样做了一个比较死板的绑定.但是结果却只能显示一条!~换编号后就有错误了!~
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim str1 As String
str1 = "select * from worker"
comm.Connection = conn
comm.CommandType = CommandType.Text
comm.CommandText = str1
da.SelectCommand = comm
da.Fill(ds, "worker")
TextBox1.DataBindings.Add("text", ds, "worker.name")
End Sub
请高手指点!~谢谢!~