我的表单上有combobox
和datatable
。 combobox
将填充datatable
的某些行值。但在此之前,我想添加一个名为(new)的combobox
项,如果用户想添加新项目,它将成为combobox
的第一个选项
但是当我的combobox
被填满时,我的(新)因某些原因不会出现。
string query = "SELECT Id,Name,Text FROM ApsisSms ORDER BY Id DESC";
OleDbDataAdapter da = new OleDbDataAdapter(query, conn);
conn.Open();
da.Fill(dtSmsMessages);
comboSMSMessages.Items.Add(new ComboboxItem() { Text = "(new)", Value = "-1" });
if (dtSmsMessages.Rows != null && dtSmsMessages.Rows.Count > 0)
{
comboSMSMessages.Items.Clear();
for (int i = 0; i < dtSmsMessages.Rows.Count; i++)
{
ComboboxItem item = new ComboboxItem()
{
Text = dtSmsMessages.Rows[i]["Name"].ToString(),
Value = dtSmsMessages.Rows[i]["Id"].ToString()
};
comboSMSMessages.Items.Add(item);
}
}
comboSMSMessages.SelectedIndex = 0;
答案 0 :(得分:2)
comboSMSMessages.Items.Clear();清除所有项目 添加comboSMSMessages.Items.Add(new ComboboxItem(){Text =“(new)”,Value =“ - 1”}); 清除组合框后,您的代码应该像
string query = "SELECT Id,Name,Text FROM ApsisSms ORDER BY Id DESC";
OleDbDataAdapter da = new OleDbDataAdapter(query, conn);
conn.Open();
da.Fill(dtSmsMessages);
if (dtSmsMessages.Rows != null && dtSmsMessages.Rows.Count > 0)
{
comboSMSMessages.Items.Clear();
comboSMSMessages.Items.Add(new ComboboxItem() { Text = "(new)", Value = "-1" });
for (int i = 0; i < dtSmsMessages.Rows.Count; i++)
{
ComboboxItem item = new ComboboxItem()
{
Text = dtSmsMessages.Rows[i]["Name"].ToString(),
Value = dtSmsMessages.Rows[i]["Id"].ToString()
};
comboSMSMessages.Items.Add(item);
}
}
comboSMSMessages.SelectedIndex = -1;