开发者

Bind Values To a comboBox Dynamically

I am Working On a C# Windows Form. I have A Combobox In My Windows Form Where I 开发者_如何学Cneed to Bind Values Dynamically Form Database Accordingly. With An Example Can Anyone explain Me How To Do It.


Get the Values of the database store them to an array or a DataSet, and by using ComboBox.DataSource Property you can bind the Combobox dynamically.

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


In a combo box it supports Name and value pair. You can use.Either

combobox1.DataSource  = ds;


combobox1.DisplayMember = "EmpName";


combobox1.ValueMember = "EmpId";

or

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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜