Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Combo Box Questions
 

Combo Box Questions

I have created a combo box from which to choose an employee.  The datasource for the combo box is a datatable.  I've set it up with 
With Me.cboEmployee
            .DataSource = dtEmployeeList
            .DisplayMember = "EmplName"
            .ValueMember = "empl_id"
End With
cboEmployee.Select(0, 0)

I have 2 questions.
1.  How do I get to the value of the valuemember empl_id?  It is the key I need to pull up all the information about the employee?
2.  If the user clicks to select the first item displayed, the selectedindexchanged event doesn't fire.  The drop down list is not changed. How can I get the selected index of that first item?

Thanks,
MigrationUser 1  Thursday, January 30, 2003 7:44 AM
to get the value member you can just use
cboEmployee.SelectedValue

or the hard way (in C#.. there's a similar thing in VB)
((DataRowView)cboEmployee.SelectedItem)[0] --> or [1] I forgot the order.. try it

ie.. cast cboEmployee.SelectedItem as a datarowview and get either the first or second column ( I forgot.. either EmplName will be col 0 or empl_id will be col 0 )
this works for me when using a dataset for the datasource.. i think it should work the same way if you're using a datatable..

dr
MigrationUser 1  Thursday, January 30, 2003 2:40 PM
Thanks dr.  I did find the selected value thing.  Also found this as an easy way to get to the information:  ctype(combobox1, SelectedValue, [Type])

Still don't know how to tell if the user selected the 1st item without dropping down the list to get the selectedindexchange event to fire.  The data loads with the index set to the first item (0).  
MigrationUser 1  Friday, January 31, 2003 7:13 AM
what do you have the DropDownStyle Property set to for your ComboBox?
MigrationUser 1  Friday, January 31, 2003 12:49 PM
Matt,

I would set the DropDownStyle Property of the Combobox to DropDownList as HumanCompiler suggested, and then use SelectionChangeCommitted instead of SelectedIndexChanged ... ( so that everytime the user clicks there, the list will always drop down and when the list closes, selection change committed will execute)

or what I once did (I think i had a similar problem to you ...) was
add an event handler for comboBox Click to include this 
comboBox.DroppedDown = true (ie force the dropdownlist to show everytime the user clicks on the combobox) 

I dunno if that's a good way of doing it or not coz I'm also pretty new to this stuff...

any other ideas ?

dr
MigrationUser 1  Friday, January 31, 2003 3:52 PM
It's set to dropdownlist.  Does that make a difference?
MigrationUser 1  Friday, January 31, 2003 4:32 PM
I think I'm having a related problem.

What I want to do is populate a combobox based on a datasource but I don't want the SelectedItemChanged event to fire until after it is populated. So I have the following in the form's constructor:

comboBox1.DataSource = myDataSource;
comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

where myDataSource is a DataSet or DataTable (either populated from a database or created by hand).

When I do this, though, the SelectedIndexChanged event fires a number of times (I think one per item being added but I haven't checked). When I create the list items manually instead (i.e. with comboBox1.Items.Add("firstItem"), etc.), the event doesn't fire, which is what I want.

When I put the code in the page's load event rather than the constructor, it works fine for this simple example, too. However, in the more complex real-world application I'm working on, it doesn't. In that application, the only thing that works is to populate the combobox in the constructor and set up the SelectedIndexChanged event in the page's load event.

Alack and alas.
MigrationUser 1  Friday, February 14, 2003 9:40 PM

You can use google to search for other answers

Custom Search

More Threads

• HELP for my ICT coursework
• datagridview cell border problem
• How to: indicate changed value in DataGridView?
• datagridviewcomboboxcolumn problem
• Hiding DataGridView selector
• Datgridview / C# problem
• datagrid and datetime value
• DataGrid Performance
• Problems while accessing DataGridViewRow's cell by its name instead of index
• CellValidating Hell