Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to avoid event trigger when loading ComboBox.DataSource
 

How to avoid event trigger when loading ComboBox.DataSource

The sistuation:

when i loading the data source of comboBox as following code, it will trigger the SelectedIndexChanged event, even I add the handler after assign the datasource.....

ds = data.GetDataSet()

dt = ds.Tables(0)
If dt.Rows.Count > 0 Then
'Me.comboBox.DataSource = dt
'Me.comboBox.DisplayMember = "name"
Me.comboBox.ValueMember = "id"
Me.comboBox.SelectedIndex = -1
End If

AddHandler comboBox.SelectedIndexChanged, _

AddressOfcomboBox_SelectedIndexChanged

How can i avoid the event when the control is binding the datasource?

Thanks,

Jerrrrrry  Friday, June 15, 2007 6:09 AM
If the comboBox_SelectedIndexChanged procedure has an handles combobox.selectedindexchanged after it delete it.
Ken Tucker  Friday, June 15, 2007 12:34 PM

hi Jerrrrrry

I found two problems about your source code:

  1. AddHandler: I think it should be RemoveHandler
  2. The Position of "AddHandler comboBox.SelectedIndexChanged, AddressOfcomboBox_SelectedIndexChanged" code, It should be the position before you bind the data to combobox

So you should write the code below:

Code Snippet

ds = data.GetDataSet()

dt = ds.Tables(0)

RemoveHandler comboBox.SelectedIndexChanged, _

AddressOfcomboBox_SelectedIndexChanged


If dt.Rows.Count > 0 Then
'Me.comboBox.DataSource = dt
'Me.comboBox.DisplayMember = "name"
Me.comboBox.ValueMember = "id"
Me.comboBox.SelectedIndex = -1
End If

I have tried the code, it work fine.

Guang-Ming Bian - MSFT  Tuesday, June 19, 2007 9:23 AM
If the comboBox_SelectedIndexChanged procedure has an handles combobox.selectedindexchanged after it delete it.
Ken Tucker  Friday, June 15, 2007 12:34 PM

hi Jerrrrrry

I found two problems about your source code:

  1. AddHandler: I think it should be RemoveHandler
  2. The Position of "AddHandler comboBox.SelectedIndexChanged, AddressOfcomboBox_SelectedIndexChanged" code, It should be the position before you bind the data to combobox

So you should write the code below:

Code Snippet

ds = data.GetDataSet()

dt = ds.Tables(0)

RemoveHandler comboBox.SelectedIndexChanged, _

AddressOfcomboBox_SelectedIndexChanged


If dt.Rows.Count > 0 Then
'Me.comboBox.DataSource = dt
'Me.comboBox.DisplayMember = "name"
Me.comboBox.ValueMember = "id"
Me.comboBox.SelectedIndex = -1
End If

I have tried the code, it work fine.

Guang-Ming Bian - MSFT  Tuesday, June 19, 2007 9:23 AM

You can use google to search for other answers

Custom Search

More Threads

• Filtering a DataGridViewComboBoxColumn
• Datagridview custom control
• show a specific row as selected in a dataGridView
• DataAdaptor help needed
• ReportViewer Table page break / KeepTogether
• How do I get the active item in a listview?
• Override the return key's default selection in datagridview
• master-detail form. CHILD records in DATAGRIDVIEW doesn't clear on adding new PARENT record
• DataGridView SelectionMode
• DataGridView: Location of a HeaderCell