简体   繁体   中英

combobox default value winforms c#

Hi here i want to bind some values to check box dynamically.

dataset ds = //getting emp values form database;
cbemp.ValueMember = "empid";
cbemp.DisplayMember = "empname";
cbemp.DataSource = ds.Tables["emp"];

it is working fine. But i want to add

"Select emplyoee" as a default value of check box .

But my check box directly adding values like

a1
a2
a3

like this.

I tried like this

cbemp.Items.Insert(0, "Select emplyoee");

but it is not working

how can i add it?

When you use databinding, you cannot "manually" add or remove items. The only way to achieve what you want using databinding is to insert a row first in the DataTable with the desired value, or to populate the combobox by code (add the "Select employee" item and then iterate of the the DataTable rows to add the records).

Perhaps something like this could work:

// create new row for "Select employee"
DataRow row = ds.Tables["emp"].NewRow();
row["empid"] = -1;
row["empname"] = "Select employee";
// insert the row at the top of the table
ds.Tables["emp"].Rows.InsertAt(row, 0);
// do the databinding
cbemp.ValueMember = "empid";
cbemp.DisplayMember = "empname";
cbemp.DataSource = ds.Tables["emp"];

I don't use databinding much, so there may be drawbacks with this that I am unaware of (but I am confident that the community will point that out in that case).

Inserting data in your data source is a bad idea. It promotes breaking your layers' abstractions and may lead to some issues if you are using the same data source elsewhere.

Instead, you can easily extend the ComboBox to show a "Please select" message when it has no item selected.

I blogged about this issue and provided the code here : http://www.byteauthor.com/2010/08/inner-label-in-combobox/

我认为您必须将它添加到基础数据表(ds.Tables [“emp”]),以便在您使用数据绑定控件时它在列表中显示为条目。

When your control is data bound you cannot add items manually I think.

To work around this problem you could either add a new item to your data source, or you could add the items manually.

如果ComboBoxStyle设置为DropDownList(因此用户无法编辑组合框),那么确保用户选择项目的最简单方法是设置selectedIndex = -1,您可以随时在组合框上添加“请选择”等。

In my case ( databinded combobox ) i solved the problem like this. Of course the best way is the Kevin Coulombe one.

ComboBox.SelectedIndex = -1;
ComboBox.Text = "Please, select something";

With a bit of code you can manage this scenario pretty easy.

The above accepted solution is better, but one trick that might come in handy sometimes is to Union a "fake" record to the SQL that is returning the recordset for the data-binding. In this case, something like:

select 0 as empid, 'Please select' as empname
union
select empid, empname from emp
order by empid

Of course you will have to protect the database from accidentally writing the "0" record back (eg if the user doesn't make a selection), but that isn't too hard.

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