Solved

Upating rows of DataTable for each Selected Item of a Listbox

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code enhancement 4 44
Convert VB web project to C# 3 65
Import a excel sheet in a grid 2 44
Whice version VisualStudio for new Dev Laptop? 10 40
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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