Solved

Combobox Help in VB.NET

Posted on 2008-10-27
3
418 Views
Last Modified: 2012-08-14
I am using VB.NET VS 2005 ..the problem is this...

I have a Combobox that is coded up this way on the Form Load event...

  strSql = "SELECT lID,sDepotDesc From tDepot"
        da = New SqlDataAdapter(strSql, con)
        da.Fill(ds, "tDepot")

        Dim dt As New DataTable
        dt.Columns.Add("lID", GetType(System.String))
        dt.Columns.Add("sDepotDesc", GetType(System.String))

        Dim drNewRow As DataRow

        For Each drDSRow In ds.Tables("tDepot").Rows()
            drNewRow = dt.NewRow()
            drNewRow("lID") = drDSRow("lID")
            drNewRow("sDepotDesc") = drDSRow("sDepotDesc")
            dt.Rows.Add(drNewRow)
        Next

        cmbo_depot1.DropDownStyle = ComboBoxStyle.DropDownList

        With cmbo_depot1
            .DataSource = dt
            .DisplayMember = "sDepotDesc"
            .ValueMember = "lID"
            .SelectedIndex = 0
        End With

I have a variable that holds the ID of the Depot when the ComboBox is changed ( which will be used to insert as part of a row in a table)...the problem is if you don't change the comboBox ...I get an error on the Insert statement because the Index hasn't changed
"The name "System.Data.DataRowView" is not permitted in this context"


this is the code that is supposed to capture the ID of the selection in the ComboBox...

   Private Sub cmbo_depot1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbo_depot1.SelectedIndexChanged
                lnPrivDepotID = cmbo_depot1.SelectedValue.ToString

    End Sub


Basically I need words displayed in the comboBox to force the user to change the combobox to pick a selection, so the index is changed  .... I used the .SelectedText to do this but I still don't see the words as the firstSelction in the ComboBox..

I don't need to do it this way....I need to be able to grab the first selection in the combobox without actual changing the combobox...the user sees the first selection and sometimes doesn't change the selection and thus it throws an error..

Any help or insight would be much appreciated...

0
Comment
Question by:nomar2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 52 total points
ID: 22815871

Two corrections:

1 - You should change the order of the bind to this:
      With cmbo_depot1
            .DisplayMember = "sDepotDesc"
            .ValueMember = "lID"
            .DataSource = dt
            .SelectedIndex = 0
        End With
 
2 - You should check if the index is valid in the SelectedIndexChanged event
Private Sub cmbo_depot1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbo_depot1.SelectedIndexChanged
         If cmbo_depot1.SelectedIndex <> -1 Then
                lnPrivDepotID = cmbo_depot1.SelectedValue.ToString
        End If

    End Sub

 
0
 

Author Comment

by:nomar2
ID: 22823769
100% PERFECT!!!!!!!!!!!!!!!!!!!!!!!!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22824171
Glad I could help!
jpaulino
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question