简体   繁体   中英

Winform Set ComboBox Placeholder

I have the following ComboBox control populated as below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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