ComboBox Does Not Clear When You Set SelectedIndex to -1

odyzeus

Member
Joined
Feb 28, 2005
Messages
6
Programming Experience
Beginner
All,

Am binding a dataset to a combobox control at form load,

Code :
Private sub form1_Load()
----
---
Try
objClientDataAdaptor.Fill(objDataSet, "Client_Name")
cboClient.DataSource = objDataSet.Tables("Client_Name")

cboClient.DisplayMember = "Client_ID"
cboClient.SelectedIndex = -1
Messagebox.show(cboClient.selectedindex) ' returns -1 as it should!

Catch ae As OleDbException
MessageBox.Show(ae.Message)

End Try
----
---
end sub

Though I am setting the selectedindex property to -1 , I am getting the first item displayed in combo box on form load.

I referred the below link that reports the BUG
http://support.microsoft.com/default.aspx?scid=kb;en-us;327244

I tried the method suggested to ,
1) execute
cboClient.selectedindex = -1
cboClient.selectedindex = -1

2) execute
cboClient.selectedindex = 0
cboClient.selectedindex = -1

None of which seem to work.

Subsequently I came across this link that explains the problems when doing a data binding with a dataset to a combo box,
http://www.windowsitpro.com/Windows/Article/ArticleID/41825/41825.html

I tried their suggested method to use a boolean m_bLoading variable to modify the firing of SelectedValueChanged event when the data values are first loaded into the control. and set the Selected index property to -1.

All with no success.

PROBLEM : Anyone can suggest me the method to CLEAR the combo box of the first item and display nothing on load.

Thnx
Odyzeus

 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Well I add a "-Select-" row to the start of my datatable before binding and then when ever I clear the contents I set the CB's .text to "-Select-"

TPM
 

odyzeus

Member
Joined
Feb 28, 2005
Messages
6
Programming Experience
Beginner
Am using the below to insert a "---Select---" into the datatable but am not able to insert it as the first row in the datatable as it gets added as the last record.

objClientDataAdaptor.Fill(objDataSet, "Client_Name")
dim objClientRow As DataRow = objDataSet.Tables("Client_Name").NewRow
objClientRow(0) = "---Select---"
objDataSet.Tables("Client_Name").Rows.Add(objClientRow)
cboClient.DataSource = objDataSet.Tables("Client_Name")
cboClient.DisplayMember = "Client_ID"
cboClient.SelectedIndex = -1

Any ideas on this, Thanx,
Odyzeus
 
Top Bottom