New to C# / Binding data to a combo box
How in the world do you bind a name,value pair to a combo box in a C# Windows Form?!?
I've been trying to figure this out for the past 8 hours and CANNOT figure it out!
this:
cboTest.Items.Add("asd");
only adds a value...
and I've Googled this thing to death....
thanks.
dd
Niradhip ChakrabortyPosted Jun 26, 2009, 1:48 AM
string strSQL = "";
string strEquipementID = "";
string Datetime = "";
SqlConnection sqlCon = null;
sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
SqlCommand sqlCmd = new SqlCommand();
SqlDataAdapter adpData = null;
SqlDataReader sdrData = null;
strSQL = "SELECT * from equipment";
cboDatetime.Items.Clear(); // Id of dropdownlist
cboDatetime.Items.Insert(0, "------- Select -------");
sqlCmd.CommandText = strSQL;
sdrData = sqlCmd.ExecuteReader();
if (sdrData.HasRows)
{
if (sdrData.Read())
{
strEquipementID = sdrData["id "].ToString();
Datetime = sdrData["dtime "].ToString();
cboDatetime.Items.Add(new ListItem(Datetime, strEquipementID));
}
}
sdrData.Close();
Note: cboDatetime is the 'id' of combobox
Eileen McGirrPosted Jun 25, 2009, 6:48 PM
shimtannyPosted Feb 14, 2005, 7:23 AM