Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 870
  • Last Modified:

Frustrated with CheckListBox getting ValueMember

It used to be so easy getting values from a listbox!!
In the code section I have the code I use to fill a checklistbox and below that the code I use to get the text portion of the selected items in the checklistbox.
I am now trying to get the ValueMember of checked items with NO LUCK!
Everything I try brings back DataRowView.
How do I get this value???
Thanks
My Fill for the checkListBox
Public Sub FillCheckedList(ByVal cnt As CheckedListBox)
        Dim ds As New DataSet
 
        If DatabaseToUse() = BizObjs.DBType.Access Then
            'Access Database
            Dim cFields As New cFieldTableNames
            cFields.GetStrTableName = msTblName
            cFields.ComboTable = msTblName
            SelectStr = cFields.ReturnComboString()
 
 
            Dim dc As New OleDb.OleDbConnection(ConnectionString)
            Dim da As New OleDb.OleDbDataAdapter(mSelectStr, dc)
          
 
            Try
                da.Fill(ds)
                dc.Close()
 
                cnt.DataSource = ds.Tables(0)
                cnt.DisplayMember = cFields.SendToCorrectFieldProcedure(cFields.ReturnDisplayField + 1)
                cnt.ValueMember = cFields.SendToCorrectFieldProcedure(1)
 
            Catch ex As Exception
 
                MessageBox.Show(ex.Message)
                Throw ex
 
            Finally
                dc.Close()
                da.Dispose()
            End Try
        End If
 
    End Sub
 
How I retrieve the text portion of the list... This works!
 
For Each i As Object In Me.lstChkSessions.CheckedItems
 
            s = Me.lstChkSessions.GetItemText(i).ToString
            ft.TabRacing.TabPages(c).Text = s
            c = c + 1
 
        Next i

Open in new window

0
Sheritlw
Asked:
Sheritlw
  • 7
  • 6
2 Solutions
 
Ken FayalCTOCommented:
You need something like this.
lstChkSessions.SelectedValue.ToString()

Open in new window

0
 
Ken FayalCTOCommented:
Woops, I gave you the C# version, but I think you get the idea.
0
 
SheritlwAuthor Commented:
I've tried that, and it doesn't work.
I think it has to do with the way I fill the checkedlistbox.
If that is the problem, then what is the best way to fill it?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ken FayalCTOCommented:
One quick way to see if the listbox is filled correctly is to do a View Source on the page from the browser.  Go to the HTML for your listbox and make sure that you see something like below.  Each <option> should have a value="something" attribute.  My guess is that you are seeing value="".

Your code looks right.  It could be that your dataset isn't being filled properly.


<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Open in new window

0
 
SheritlwAuthor Commented:
It is filling it correctly, I just can't get the values I need.  I also read somewhere that valuemember isn't used for the checklistbox, but I don't get an error with it???
0
 
funwithdotnetCommented:
Try something like this:
Dim myListItem as ListItem
For each myListItem in MyCheckBoxList.Items
If myListItem .Selected Then
' myListItem.Value is available
End If
Next

Open in new window

0
 
Ken FayalCTOCommented:
Wait.. my bad.  I didn't read you correctly  You are using a checklistbox, not a regular listbox.  Try this...
For i = 0 To (lstChkSessions.Items.Count - 1)
   If lstChkSessions.GetItemChecked(i) = True Then
      s = lstChkSessions.Items(i).ToString
   End If
Next

Open in new window

0
 
SheritlwAuthor Commented:
Both of those return DataRowView.  
That is why I think it must be the way I fill the checkedlistbox.
Could you provide an example on how to fill this type of checkedlistbox?
thanks
0
 
Ken FayalCTOCommented:
Ok, I see now..

Your code is filling the data items with objects instead of just the text portion

I don't know what your SendToCorrectFieldProcedure() method is actually doing but it's returning DataRowView.  You need to get the text value out of it instead of returning the actual datarowview object.
cnt.DataSource = ds.Tables(0)
cnt.DisplayMember = cFields.SendToCorrectFieldProcedure(cFields.ReturnDisplayField + 1)
cnt.ValueMember = cFields.SendToCorrectFieldProcedure(1)
 
'Try it this way
 
cnt.DataSource = ds.Tables(0)
cnt.DisplayMember = cFields.SendToCorrectFieldProcedure(cFields.ReturnDisplayField + 1).ToString
cnt.ValueMember = cFields.SendToCorrectFieldProcedure(1).ToString()

Open in new window

0
 
Ken FayalCTOCommented:
Simply put, you want give the cnt.DataSource a table, which you have done, and you also want to give the displaymember and valuemember a text value representing a field name in the table you provided.
0
 
SheritlwAuthor Commented:
That's exactly how I have it.  I have been looking all over the net and it appears that I am supposed to use Add.Item then something when retrieving the data.
In VB 2008 Value and Displaymember are not used, but there for old timers like me.  They don't provide the same flexibility as the add.items.
I am trying to find out how to use the add to fill this checkedlistbox from a dataset.
Any info will help
thanks
0
 
Ken FayalCTOCommented:
Ok, you've got to access the value member of the datarow view then.  Here check this out - short article that I think will answer your questions..

http://www.devx.com/tips/Tip/30427

0
 
SheritlwAuthor Commented:
I would like to try that.  Can you convert to VB.Net please.
I am new to this .Net stuff and VB.Net is all I've worked on thus far, besides 10 + years in VB

Thanks
0
 
SheritlwAuthor Commented:
I found the solution and what a pain... sometimes I miss VB 6
Solution is I have to declare a datarowview
Heres the code
 For Each i As Object In Me.lstChkSessions.CheckedItems

            s = Me.lstChkSessions.GetItemText(i).ToString
            ft.TabRacing.TabPages(c).Text = s
            Dim view As DataRowView = Me.lstChkSessions.CheckedItems(c)
            ft.TabRacing.TabPages(c).Tag = view.Item("SessionTypeID")

            c = c + 1

        Next i

thanks for your help Kaptainkenbo... I will award you the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now