Solved

Upating rows of DataTable for each Selected Item of a Listbox

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

Accepted Solution

by:
louisfr earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now