combobox datasource

0
0

Hello. I have a combobox in my project. When I write like this:

var combobox = new ComboBox();

var list= new List<ListBoxKeyValue>();

var row= new ListBoxKeyValue() { Key = “dropDownRow”, Value = “dropDownRow” };

list.Add(row);

combobox.DataSource = list;

list appears in the dropdown list, however the first element of my list is always selected by default. I want my text remains to remain empty and selectedItem to remain null. Is there a way to fix it?

 

  • You must to post comments
0
0

Hi Dmitry,

This is the same behavior as in WinForms.  I would recommend setting the SelectedIndex to -1 or the SelectedItem to null after setting your DataSource and handling the case specifically in your SelectedIndexChanged event handler.  You can also do ResetText() to clear the text.

Hope that helps, let me know if you have any other questions!

Best,

Levie

  • You must to post comments
0
0

public class ListBoxKeyValue
{
public string Key;
public string Value;
public bool IsDateTime;
public override string ToString()
{
if (string.IsNullOrEmpty(Value))
return Key;
else
return Value;
}
}

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.