将值绑定到组合框动态
问题描述:
我正在C#Windows窗体上工作。 我有一个Combobox在我的Windows窗体中,我需要绑定值动态地形成数据库相应。 举个例子任何人都可以解释我如何做到这一点。将值绑定到组合框动态
答
获取数据库的值将它们存储到数组或数据集中,并且使用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";
答
在组合框,它支持名称和值的对。 您可以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.
u能PLSS解释用一个例子 – Lijina
请参阅编辑代码 –