Wrox Programmer Forums
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2003, 12:54 PM
Registered User
 
Join Date: Nov 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Databound List Box

Hi All,

I have two listboxes (we'll call lstTarget and lstSource). Both are set to MultiExtended and bound to a datatable. I want to be able to add multiple items from lstSource to lstTarget (which works fine) and then remove the selected items from lstSource. So far it only works for continuously selected items, but a logic error occurs when you select non-continuous items.

Apparently you have to make changes to the underlying datatable, not the listbox, however, the lstSource.Remove(objRow) generates an exception.

Thanks,
Kunkel


 
Old November 7th, 2003, 01:09 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

1. What's the error details?
2. Can you post the code you are using to move the items?

You should only need to change the underlying datasource if you rebind the listboxs after making changes.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 7th, 2003, 01:38 PM
Registered User
 
Join Date: Nov 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

here's the working code that moves succesfully, it would be nice to add and remove the row in the same for loop:
-------------
        'this for loop adds item(s) from SOURCE to TARGET
        Dim objDataRowView As DataRowView
        For Each objDataRowView In lstSource.SelectedItems
            'ADD A ROW FROM THIS SOURCE TABLE TO THE TARGET TABLE
            Dim objRow As DataRow = objDataRowView.Row
            f_dtTarget.ImportRow(objRow)

            'the next line generates an exception
            'f_dtSource.Rows.Remove(objRow)
        Next
-------------
i have also tried creating several remove loops after the move loop:
---------------
attempt1->exception-index out of range (there is no row at -1)

        Dim intLastIndex
        For intLastIndex = lstSource.SelectedItems.Count - 1 To 0 Step -1
            f_dtSource.Rows.RemoveAt(lstSource.SelectedItems.I ndexOf(objDataRowView))
        Next
-----------------
attempt2->exception-Cannot modify the Items collection when the datasource property is set.

        Dim intTotalSourceRows As Integer = f_dtSource.Rows.Count - 1
        Dim i As Integer
        Dim intTotalTargetRows As Integer = f_dtTarget.Rows.Count - 1
        Dim j As Integer

        For i = 0 To intTotalTargetRows
            'set the index of target from loop counter
            lstTarget.SelectedIndex() = i
            For j = 0 To intTotalSourceRows
                'set the index of source from loop counter
                lstSource.SelectedIndex() = j
                'if selected values are equal then
                If lstTarget.Items(i).row("Thing") = lstSource.Items(j).row("Thing") Then
                    'remove row from source
                    f_dtSource.AcceptChanges()

                    lstSource.Items.RemoveAt(j)
                End If
            Next
        Next
---------
thanks for help
Kunkel






Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab Values From List Box into Text Box phungleon VB How-To 2 June 19th, 2008 10:33 PM
multi-column list box values moved to 2nd list box sbmvr Access VBA 1 May 14th, 2007 01:58 PM
select box/List box alphabetic sort sasidhar79 Javascript How-To 3 November 10th, 2004 03:04 AM
Populate List Box by Combo Box Selection mmcdonal Access 2 June 15th, 2004 12:08 PM
Search using drop down list box and a text box tcasp Classic ASP Basics 1 July 31st, 2003 02:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.