2011-12-13 58 views
0

如何pupolate在VB.Net組合框,該項目從DataSet中...這裏是我的代碼如何填充組合框在VBdotNet

Dim LibDSPopulate As New DataSet 
     Dim LibDAPopulate As OdbcDataAdapter = New OdbcDataAdapter("SELECT DISTINCT Category FROM tblBooks", LibConn) 
     LibDAPopulate.Fill(LibDSPopulate, "tblBooks") 

     cmbCategoryF.Items.Add(LibDSPopulate) 

回答

1

您可以使用數據綁定facility

'Add an empty entry 
Dim dr As DataRow = LibDSPopulate.Tables("tblBooks").NewRow 
dr("Category") = "***Select***" 
LibDSPopulate.Tables("tblBooks").Rows.InsertAt(dr, 0) 

cmbCategoryF.DataSource=LibDSPopulate.Tables("tblBooks") 
cmbCategoryF.DisplayMember="Category" 'Name of field 
cmbCategoryF.ValueMember="Category" 'Name of field 

如果您不想使用databindng,則使用Items.Add()方法添加每個項目。

 For Each row As DataRow In LibDSPopulate.Tables("tblBooks").Rows 
    ComboBox1.Items.Add(row("Category")) 
Next 
+0

使用DataBinding ...空項(位於c​​mbCategoryF中的0行)來自哪裏?我試圖刪除它...並感謝您提供的教程... – aer

+1

@aerohn - 如果您使用dataBinding,則必須在DataTable中添加一個空條目。看到我編輯的帖子。 – adatapost

+1

是的,呃! 我真的很喜歡在編程中學習新東西:) – aer