Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Upating rows of DataTable for each Selected Item of a Listbox

Posted on 2016-11-04
3
Medium Priority
?
64 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
  • 2
3 Comments
 
LVL 12

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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