我有一个组合框,用于填充来自数据库的数据。我想要默认文本 首次加载页面时,“请选择”显示在组合框中,重置时必须位于顶部。只打开组合框时出现默认文本。请帮忙
这是我如何绑定我的组合框;
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["Name"].ToString();
cboDepartment.Items.Add(_Combobox);
}
cboDepartment.Items.Insert(0, "--Select Department--");
oReader.Close();
oConnection.Close();
}
catch(Exception ex)
{
}
}
答案 0 :(得分:1)
试试这个代码段,
cboDepartment.Items.Add("--Select Department--");
while (oReader.Read())
{
string _Combobox = oReader["Name"].ToString();
cboDepartment.Items.Add(_Combobox);
}
cboDepartment.selectedIndex=0;
答案 1 :(得分:0)
创建字符串列表并使用组合框绑定。
try
{
oConnection = new SqlConnection(_connectionString);
oCommand = new SqlCommand("select * from tbldpt", oConnection);
oConnection.Open();
oDataset = new System.Data.DataSet();
SqlDataReader oReader = oCommand.ExecuteReader();
list<string> _combobox=new list<string>();
_combobox.add("--Select Department--");
while (oReader.Read())
{
_combobox.Add(oReader["Name"].ToString());
}
cboDepartment.Datasource=_combobox
oReader.Close();
oConnection.Close();
}
catch(Exception ex)
{
}