Winform Set ComboBox占位符

时间:2014-05-23 06:02:00

标签: c# winforms combobox

我有以下的ComboBox控件填充如下

DataTable dt2 = InfoPCMS.db.executeSelectQuery("select * from Customer");

txtCustomer.DataSource = dt2;
txtCustomer.ValueMember = "Id";
txtCustomer.DisplayMember = "CustomerName";

如何设置占位符说"选择客户"

1 个答案:

答案 0 :(得分:0)

这不是你问题的答案,但它会解决你的问题。我认为有更好的方法来完成这项工作,但这很有效:

DataTable dt2 = InfoPCMS.db.executeSelectQuery("select * from Customer");

foreach (DataRow row in dt2.Rows)
{
    ComboboxItem newItem = newComboboxItem(row["id"], row["CustomerName"]);
    txtCustomer.Items.Add(newItem);
}
txtCustomer.Items.Insert(0,"Select a customer");
txtCustomer.SelectedIndex = 0;

public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public ComboboxItem(object val, string txt)
    {
        this.Value = val;
         this.Text = txt;
    }
    public override string ToString()
    {
        return Text;
    }
}