![]() |
Click here to advertise with us
|
|
|||||||
| Windows Forms Discussion related to Winforms application development |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
I'm loading a combobox from a stored procedure, which appears to be working fine(see code below). So now I want to get the ValueMember from the combobox when the user selects a value and here's where my problem starts.
I figured I could get the ValueMember from the SelectedIndexChanged event by using the following: comboboxValue = Me.cboCategory.SelectedValue When I use this code I get an error (Argument 'Prompt' cannot be converted to type 'String'.) during what appears to be the loading of the combobox. I can however remove that code, add a button to the form and after a value is selected from the combobox, click the button and the same code returns the correct value. I next tried to drop a message box to see the SelectedIndex value in the SelectedIndexChanged event and to my amazement it appeared that during the loading of the combo box the message box show three values, being 0,0 and -1 respectively. I next tried the Click event of the combobox adding a messagebox returning the msgbox(SelectedIndex), but as soon as I click on the drop down arrow on the combo box the message box comes up with a -1 value and closes the combo box. So the questions are: 1. Am I loading the combobox correctly from the stored procedure? 2. Why is the combobox returning index values (0,0,-1) during the load? 3. Why is the Click event return -1 when I click the drop down on the combobox? If I'm doing something wrong could someone give me some code examples on how to return the ValueMember from the combobox and which event should I use. Thanks, Tom Public Function LoadCategories(ByVal cboBox As ComboBox) As Boolean Dim cmdsub As New SqlCommand Dim da As New SqlDataAdapter(cmdsub) Dim ds As New DataSet() If objConn.State <> ConnectionState.Open Then objConn = objConnClass.GetConnection End If cmdsub.Connection = objConn cmdsub.CommandText = "GetVendorCategories" cmdsub.CommandType = CommandType.StoredProcedure cmdsub.Parameters.Add("@rc", SqlDbType.Int).Direction = ParameterDirection.ReturnValue Try da.Fill(ds) da.Fill(ds, "Disk") With cboBox .DataSource = ds.Tables("Disk") .DisplayMember = "VendorCategory" .ValueMember = "vcID" .SelectedIndex = -1 End With return true Catch ex As SqlException MsgBox("Error Encountered In Loading Vendor Categories." & ex.Message, MsgBoxStyle.OkOnly, "Error") Return false cmdsub.Dispose() objConn.Dispose() End Try End Function |
|
||||
|
The SelectedIndexChanged event is raised every time the SelectedIndex property value changes. If your ComboBox is initially empty then its SelectedIndex is -1. When you bind data to it the first item is selected by default and the SelectedIndex changes to 0. You are then explicitly setting the SelectedIndex to -1 again.
If you're only interested in knowing when the user makes a selection then handle the SelectionChangeCommitted event instead.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms MSDN: Data Walkthroughs | "How Do I?" Videos My Blog: Custom Events | Dynamic GDI+ Drawing |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|