Solved

Combobox Help in VB.NET

Posted on 2008-10-27
3
416 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

838 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