Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Upating rows of DataTable for each Selected Item of a Listbox

Posted on 2016-11-04
3
Medium Priority
?
62 Views
Last Modified: 2016-11-07
        Using cn As New SqlConnection(strConn)
            Using da As New SqlDataAdapter(m_strSQL, cn)
                da.Fill(dt)
            End Using
        End Using

        Me.Text = m_strTitle
        bs1.DataSource = New DataView(dt)
        bs1.Sort = "Name"
        bs1.Filter = "Selected = 0"
        lbxUnSelectedItems.DataSource = bs1
        lbxUnSelectedItems.ValueMember = "Id"
        lbxUnSelectedItems.DisplayMember = "Name"

        bs2.DataSource = New DataView(dt)
        bs2.Sort = "Name"
        bs2.Filter = "Selected = 1"
        lbxSelectedItems.DataSource = bs2
        lbxSelectedItems.ValueMember = "Id"
        lbxSelectedItems.DisplayMember = "Name"
        tbxQtySelected.Text = lbxSelectedItems.Items.Count

Open in new window


What code can I write to update the "Selected" field of dt to "1" for every item in the lbxUnSelectedItems.SelectedItems collection?  (Note: lbxUnSelectedItems is a listbox)
0
Comment
Question by:Declan_Basile
[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 11

Accepted Solution

by:
louisfr earned 2000 total points
ID: 41876976
foreach(DataRowView d in lbxUnSelectedItems.SelectedItems.Cast<DataRowView>().ToList())
{
    ((DataSet1.DataTable2Row)d.Row).Selected = 1;
}

Open in new window

Calling ToList is important because you're modifying the SelectedItems collection, so you need to work on a copy of it.
0
 
LVL 1

Author Comment

by:Declan_Basile
ID: 41877547
I'm trying to convert this to vb.net and didn't find a "ToList" method.  Any ideas?

        For Each drv As DataRowView In lbxUnSelectedItems.SelectedItems
            drv.Row("Selected") = 1
        Next
0
 
LVL 1

Author Closing Comment

by:Declan_Basile
ID: 41877662
Here's the vb.net code that worked:
        For Each drv As DataRowView In lbxUnSelectedItems.SelectedItems.Cast(Of DataRowView).ToList
            drv.Row("Selected") = 1
        Next
You convert the SelectObjectsCollection to an array of DataRowView using the Cast method, then create a list from that.  The DataRow of the underlying datatable is exposed by the Row property of the DataRowView object.

Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 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