Solved

Upating rows of DataTable for each Selected Item of a Listbox

Posted on 2016-11-04
3
55 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 500 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

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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