0

我在组合框中出现错误,而不是组合框显示数据库...它在里面显示 combobox.text = "System.Data.DataRowView"。

Private Sub FillCombo()
Try
    conn = New OleDbConnection(Get_Constring)
    Dim sSQL As String = ("SELECT subject FROM student order by subject")
    Dim da As New OleDbDataAdapter(sSQL, conn)
    Dim ds As New DataSet
    da.Fill(ds)
    cmbsection.ValueMember = "ItemName"
    cmbsection.DataSource = ds.Tables(0)
    cmbsection.SelectedIndex = 0
Catch ex As Exception
    MsgBox("ERROR : " & ex.Message.ToString)
End Try

结束子

4

5 回答 5

0

Try this

Private Sub FillCombo()
Try
   conn = New OleDbConnection(Get_Constring)
   Dim sSQL As String = ("SELECT subject FROM student order by subject")
   Dim da As New OleDbDataAdapter(sSQL, conn)
   Dim ds As New DataSet
   da.Fill(ds)
   cmbsection.ValueMember = "ItemName"
   cmbsection.DataSource = ds;
   cmbsection.SelectedIndex = 0;
Catch ex As Exception
   MsgBox("ERROR : " & ex.Message.ToString)
End Try
于 2013-10-01T09:22:51.557 回答
0
 Private Sub FillCombo()
Try
    conn = New OleDbConnection(Get_Constring)
    Dim sSQL As String = ("SELECT subject FROM student order by subject")
    Dim da As New OleDbDataAdapter(sSQL, conn)
    Dim ds As New DataSet
    da.Fill(ds)
cmbsection.Items.clear
For i as Integer = 0 to ds.Tables(0).rows.count-1
    cmbsection.Items.add( ds.Tables(0).rows(i).item("subject").tostring)

next

Catch ex As Exception
    MsgBox("ERROR : " & ex.Message.ToString)
End Try
End Sub
于 2013-10-01T10:00:06.163 回答
0

您需要设置

cmbsection.DisplayMember = "Subject"
cmbsection.ValueMember = "Subject"
于 2013-10-01T14:29:58.507 回答
0

我认为这是在 Valuemember 之前设置 Datasource 的简单案例。

cmbsection.DataSource = ds.Tables(0)
cmbsection.ValueMember = "ItemName"
于 2013-10-01T09:17:38.883 回答
0
Public Sub LoadProvince()
    Dim cn As New SqlConnection("server=.\LENOVO;uid=sa;pwd=123;database=SchoolDb;")
    cn.Open()

    Dim cmd As New SqlCommand("SELECT * FROM Product;", cn)
    Dim dr = cmd.ExecuteReader()

    Dim dt As New DataTable()
    dt.Load(dr)
    dr.Close()

    cboPROVINCE.DisplayMember = "Product_NAME"
    cboPROVINCE.ValueMember = "Product_Id"
    cboPROVINCE.DataSource = dt
End Sub
于 2014-03-23T07:00:52.643 回答