2011-09-29 135 views
-1

我正在C#Windows窗體上工作。 我有一個Combobox在我的Windows窗體中,我需要綁定值動態地形成數據庫相應。 舉個例子任何人都可以解釋我如何做到這一點。將值綁定到組合框動態

回答

3

獲取數據庫的值將它們存儲到數組或數據集中,並且使用ComboBox.DataSource屬性可以動態地綁定組合框。

EDIT

string[] stringArray = { "one", "two", "three", "four" }; 
comboBox1.DataSource = stringArray; 

      OR 
SqlCommand cmd = new SqlCommand("Select StdNo,StdName from TempDb", conn); 
conn.Open(); 
SqlDataAdapter DataA = new SqlDataAdapter(cmd); 
DataTable DSet = new DataTable(); 
DataA.Fill(DSet); 
conn.Close(); 
ComboBox1.DataSource = DSet; 
ComboBox1.DisplayMember = "StdName"; 
ComboBox1.ValueMember = "StdNo"; 
+0

u能PLSS解釋用一個例子 – Lijina

+0

請參閱編輯代碼 –

1

在組合框,它支持名稱和值的對。 您可以use.Either

combobox1.DataSource = ds; 


combobox1.DisplayMember = "EmpName"; 


combobox1.ValueMember = "EmpId"; 

Dim str As String 
     str = "Select * from CountryTable" 
     ddCountry.DataSource = obj.GetDataSet(str) 
     ddCountry.Items.Clear() 
     ddCountry.DataValueField = "COUNTRYID" 
     ddCountry.DataTextField = "COUNTRYName" 
     ddCountry.DataBind() 

//GetDataSet is a function which returns a dataset.