2013-10-01 19 views
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 

末次

回答

0

我覺得這是設置Valuemember前的數據源的簡單情況。

cmbsection.DataSource = ds.Tables(0) 
cmbsection.ValueMember = "ItemName" 
+0

歡聲笑語。我編碼太長,這讓我很困惑,錯過了一個簡單的問題:) 順便問一下,我可以問我怎麼可以過濾我的組合框顯示,其中SUBJECT包含2是「COE和IT」,如果我是COE,它應該只顯示所有COE並且不會顯示IT。 – user2715202

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.ValueMember = "ItemName" 
    cmbsection.DataSource = ds; 
    cmbsection.SelectedIndex = 0; 
Catch ex As Exception 
    MsgBox("ERROR : " & ex.Message.ToString) 
End Try 
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 
0

你需要設置這是reaally漏看的

cmbsection.DisplayMember = "Subject" 
cmbsection.ValueMember = "Subject" 
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