How to delselect items in databound ListBox or ComboBox

A

andersonwebdev

When I use the DataSource property of a ListBox or ComboBox, there is
always a selected item. When I bind the control, I don't want anything
selected. I have tried using the .SetSelected() method, setting,
selectedIndex to -1 (twice in a row, blah blah), setting SelectedItem
to null, etc.

listBoxInventorParts.DataSource = _selectedInventorParts;
listBoxInventorParts.DisplayMember = "InventorPartNumber";
listBoxInventorParts.ValueMember = "pkInventorPartId";

// Doesn't work
listBoxInventorParts.SelectedIndex = -1;

// Doesn't work
listBoxInventorParts.SelectedItem = null;

// Doesn't work
listBoxInventorParts.SelectedValue = null;

// Doesn't work
listBoxInventorParts.ClearSelected();

// Doesn't work
for(int i = 0; i < listBoxInventorParts.Items.Count; i++)
{
listBoxInventorParts.SetSelected(i, false);
}

Nice.
 
A

andersonwebdev

Found the answer. It's a bug in my view. I was doing the databinding
in Form_Load, and attempting to deselect items in the Load event did
not work. I discovered that to deselect any items, I had to do it in
the Form_Paint event:

private void InventorAssemblyTreeStep3_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
listBoxInventorParts.SelectedItem = null;
}

- Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top