Visual Basic,填充组合框

时间:2013-12-03 07:05:29

标签: vb.net sql-server-2008

这段代码给了我在combobox中的system.data.datarowview show.But我想用数据填充组合框。什么是错误?

Dim adclasstecher As New SqlDataAdapter
Dim tbclassteacher As New DataTable
If cnSchool.State = ConnectionState.Closed Then cnSchool.Open()
cmclassteacher.Connection = cnSchool
cmclassteacher.CommandText = "SELECT * FROM Teachers"
cmclassteacher.ExecuteNonQuery()

adclasstecher.SelectCommand = cmclassteacher
adclasstecher.Fill(tbclassteacher)

With cmbclzteachr
    .DataSource = tbclassteacher
    .DisplayMember = "ClassTeacher"
    .SelectedIndex = 0
End With
cnSchool.Close()

1 个答案:

答案 0 :(得分:0)

Dim adclasstecher As New SqlDataAdapter
Dim tbclassteacher As New DataTable
If cnSchool.State = ConnectionState.Closed Then cnSchool.Open()
cmclassteacher.Connection = cnSchool
cmclassteacher.CommandText = "SELECT * FROM Teachers"
cmclassteacher.ExecuteNonQuery()

adclasstecher.SelectCommand = cmclassteacher
adclasstecher.Fill(tbclassteacher)

With cmbclzteachr
    .DataSource = tbclassteacher
    .DisplayMember = "ClassTeacher"
    .SelectedIndex = 0
    .databind() //you need to bind the data after providing datasource
End With

cnSchool.Close()