English 中文(简体)
How to prevent a combobox first item from being selected on DataSet.Merge()
原标题:

I ve got a ComboBox which items are coming from a DataTable in a DataSet. When there is no item selected (combo.selectedindex = -1) and I do a .Merge() on the DataSet the first element of the combobox is selected. When there is an item already selected, the item stay selected.

How can I prevent this and make it so that even if I do a MyDataSet.Merge(otherDs) the selectedIndex remains at -1 (no selection)?

最佳回答

A combo box is really designed to avoid having no selection. As you ve seen it is possible to get it in that state by setting SelectedIndex to -1, but that s not really the metaphor the ComboBox designers were going for. The idea is that the user is restricted to the combo box values. So if "Nothing Selected" is a valid value for the box (or at least valid for the initial load) it needs to be part of your data table. It s common to have a row with ValueMember column as DBNull.Value and the DisplayMember as "Nothing Selected" or "Please select a value" or whatever.

Then your UI validation can ensure they didn t leave it on that value.

UPDATE: You can always add the value last minute if you can t add the value from the data source procedure. In fact, some might consider this a good idea since this "nothing selected" option is purely a UI state.

DataRow nullRow = MyDataSet.MyTable.NewRow();
nullRow["VAL_COLUMN"] = DBNull.Value;
nullRow["DISP_COLUMN"] = "Please select a value...";
MyDataSet.MyTable.Rows.Insert(0, nullRow); //might have those parameters backwards..
问题回答

暂无回答




相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签