动态地将值绑定到组合框

时间:2011-09-29 04:18:32

标签: c# data-binding combobox

我正在使用C#Windows窗体。 我在我的Windows窗体中有一个Combobox,我需要动态地绑定值表格数据库。 有一个例子,任何人都可以解释我如何做到这一点。

2 个答案:

答案 0 :(得分:3)

获取数据库的值将它们存储到数组或DataSet中,并使用ComboBox.DataSource属性动态绑定Combobox。

修改

 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";

答案 1 :(得分:1)

在组合框中,它支持名称和值对。 你可以使用。

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.