Bheeshamteche
Bheeshamteche

Reputation: 47

first element is coming selected by default in list box

i am creating windows application. I am binding listbox with database. It is loading and first item is coming as selected. But I don't want this. How can I deselect by-default item on loading in listbox

ListBox1.DataSource = dt;
ListBox1.DisplayMember = "JobName";
ListBox1.ValueMember = "JobName";

how can i solve this problem

Upvotes: 2

Views: 3457

Answers (3)

Triple K
Triple K

Reputation: 399

Try this,

listBox1.SelectedValue = null;

Hope it solves.

Upvotes: 0

Stack Over
Stack Over

Reputation: 7

ListBox1.SelectedIndex = -1;

this will works

Upvotes: 0

Habib
Habib

Reputation: 223237

You can do:

ListBox1.SelectedIndex = -1;

Or

ListBox1.ClearSelected();

See: ListBox.ClearSelected Method

Calling this method is equivalent to setting the SelectedIndex property to negative one (-1). You can use this method to quickly unselect all items in the list.

Remember to call any of the above after assigning your DataSource

ListBox1.DataSource = dt;
ListBox1.DisplayMember = "JobName";
ListBox1.ValueMember = "JobName";
ListBox1.SelectedIndex = -1;

Upvotes: 2

Related Questions