简体   繁体   English

Winform Set ComboBox占位符

[英]Winform Set ComboBox Placeholder

I have the following ComboBox control populated as below 我有以下填充的ComboBox控件,如下所示

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

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

How can I set a placeholder saying "Select a customer" 如何设置占位符,说“选择客户”

This is no an answer to your question, but It will solve your problem. 这不能解决您的问题,但可以解决您的问题。 I think there is a better way to do the job, but this works: 我认为有一种更好的方法可以完成这项工作,但这可行:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM