Hi ,
i have a combo box which loads from the database. Now i want default text on the combo box ("--Please Select--") to appear on top of the list, it must show the text whithout the user even opened the combo box. Also when the user clear the combo box, the default text will be "--Please Select--"
this is how i bindeed it ;
private void LoadCombo()
{
try
{
oConnection = new SqlConnection(_connectionString);
oCommand = new SqlCommand("select * from tbldpt", oConnection);
oAdapter = new SqlDataAdapter(oCommand);
oConnection.Open();
oDataset = new System.Data.DataSet();
SqlDataReader oReader = oCommand.ExecuteReader();
while (oReader.Read())
{
string _Combobox = oReader["Departmentname"].ToString();
cboDepartment.Items.Add(_Combobox);
}
oReader.Close();
oConnection.Close();
}
catch(Exception ex)
{
}
}
VulpesPosted Dec 4, 2014, 6:41 AM
EnosPosted Dec 4, 2014, 5:25 AM
DataRow row = oDataset.Tables[0].NewRow();
row["Category"] = "--Please Select--";
oDataset.Tables[0].Rows.InsertAt(row, 0);
Manish Kumar ChoudharyPosted Dec 4, 2014, 4:57 AM
private void LoadCombo()
{
try
{
oConnection = new SqlConnection(_connectionString);
oCommand = new SqlCommand("select * from tbldpt", oConnection);
oAdapter = new SqlDataAdapter(oCommand);
oConnection.Open();
oDataset = new System.Data.DataSet();
SqlDataReader oReader = oCommand.ExecuteReader();
DataRow row = oDataset.Tables [0].NewRow();
row["Category"] = "--Please Select--";
oDataset.Tables[0].Rows.InsertAt(row, 0);
while (oReader.Read())
{
string _Combobox = oReader["Departmentname"].ToString();
cboDepartment.Items.Add(_Combobox);
}
oReader.Close();
oConnection.Close();
}
catch (Exception ex)
{
}
}
EnosPosted Dec 4, 2014, 4:30 AM
Manish Kumar ChoudharyPosted Dec 4, 2014, 4:23 AM
this will work fine
private void LoadCombo()
{
try
{
oConnection = new SqlConnection(_connectionString);
oCommand = new SqlCommand("select * from tbldpt", oConnection);
oAdapter = new SqlDataAdapter(oCommand);
oConnection.Open();
oDataset = new System.Data.DataSet();
SqlDataReader oReader = oCommand.ExecuteReader();
while (oReader.Read())
{
string _Combobox = oReader["Departmentname"].ToString();
cboDepartment.Items.Add(_Combobox);
}
cboDepartment.Items.Insert(0, "--Please Select--");
oReader.Close();
oConnection.Close();
}
catch (Exception ex)
{
}
}