如何设置标签的文本等于数据绑定列表框的选定项?

时间:2013-03-26 19:00:18

标签: vb.net

我已将ListBox的DisplayMember设置为“Employee”列,将ValueMember设置为“EmpID”列。列表框填充正常,但标签永远不会更改。我是新手,无法弄清楚我错过了什么。这是我的代码:

Me.EmployeeTableAdapter.Fill(Me.TimeCards2DataSet.Employee)

Label1.Text = lstEmployees.SelectedItem.ToString

1 个答案:

答案 0 :(得分:0)

您可能正在寻找SelectedIndexChanged事件:

Private Sub lstEmployees_SelectedIndexChanged(sender As Object, e As EventArgs) _
                         Handles lstEmployees.SelectedIndexChanged
  If lstEmployees.SelectedItem Is Nothing Then
    Label1.Text = String.Empty
  Else
    Label1.Text = lstEmployees.SelectedValue.ToString
  End If
End Sub

或者,如果使用DataBinding:

Label1.DataBindings.Add("Text", Me.TimeCards2DataSet.Employee, "Employee")