Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with adding data to listbox

Hi,

My list box already contains data that was loaded using a dataset. When I try to add more data with the code below from a button click event:

 Dim item As String
        item = C1NSNA.Text
        C1NSN.Items.Add(item)

I receive the folowing error:

Items collection cannot be modified when the DataSource property is set.

How do i fix this error?

Victor
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

When you set the data source for a ListBox or ComboBox via the .DataSource property, you are in fact telling .Net that your list control must show the items provided in the datasource, so you cannot manually add or remove items. So you got 2 possible solutions here:

1. Don't fill the list via the .DataSource property; instead of it, iterate the dataset in a for/next and add manually each item to the list.

or

2. If you still want to use the .DataSource, then add previously rows to the dataset with the values that you want in your list.

Hope that helps.
Avatar of Victor  Charles

ASKER

Can you please send me an example of option2.
Once you got your dataset filled with rows, you can do the following:

Dim row As DataRow = myDataSet.Tables(0).NewRow()
row("column 1") = "Value 1"
row("column 2") = 22
row("column 3") = "another value"
...
myDataSet.Tables(0).Rows.Add(row)

And after that, you can bind your Dataset with your ListBox.

Hope that helps.
Hi again,

I used the code  in part A of my web project and it works, any ideas why it doesn"t work in the Windows version? The listbox is populated same way in both web and Windows version,
as shown in part B.


Part A:
   Dim item As ListItem = New ListItem()
           item.Text = C1NSNA.Text
        C1NSN.Items.Add(item)


Part B

Dim _table As DataView = dtMaster.DefaultView
            _table.RowFilter = "NSN_ID in (" & keyNsn & ")"
            C1NSN.DataSource = _table
            C1NSN.DataTextField = "NSN"
            C1NSN.DataValueField = "NSN_ID"
            C1NSN.DataBind()
Because the Windows Forms ListBox inherits from System.Windows.Forms.ListControl and the Web Forms ListBox inherits from System.Web.BaseDataBoundControl, that obviously are not the same. If you open the Windows Forms ListBox link, you'll see in the MSDN page:
"When the DataSource property is set, a user cannot modify the items collection."

This behaviour doesn't happen in Web Forms.
I tried the code below but getting error message on the third line ( Tables is not a memeber of System.Data.Datatable)

  Dim row As DataRow = dtMaster.NewRow()
        row("NSN") = C1NSNA.Text
        dtMaster.Tables(0).Rows.Add(row)
        C1NSN.Items.Add(row)

The Listbox was loaded using the code below

Dim _table As DataView = dtMaster.DefaultView
            _table.RowFilter = "NSN_ID in (" & keyNsn & ")"
            C1NSN.DataSource = _table
            C1NSN.DataTextField = "NSN"
            C1NSN.DataValueField = "NSN_ID"
            C1NSN.DataBind()
Ok, you're binding without a dataset, but with a dataview that internally contains a datatable.

Instead of:
dtMaster.Tables(0).Rows.Add(row)
Add this:
dtMaster.Rows.Add(row)

You must do the above code before the .DataBind part.

And you don't need this:
C1NSN.Items.Add(row)

Hope that helps.
Hi,

I executed ViewDataB before the code as you suggested. i get no errors, but no data is copied to the listbox.

 Private Sub ViewDataB(ByVal keyLinkAid As String, ByVal keyNsn As String)
        Dim _table As DataView = dtMaster.DefaultView
        _table.RowFilter = "NSN_ID in (" & keyNsn & ")"
        C1NSN.DataSource = _table
        C1NSN.DisplayMember = "NSN"
        C1NSN.ValueMember = "NSN_ID"
    End Sub

 Private Sub ANSN_Click(sender As System.Object, e As System.EventArgs) Handles ANSN.Click
        ViewDataB(dtKeyMaster.Rows(0)(1), dtKeyMaster.Rows(0)(2))
        Dim row As DataRow = dtMaster.NewRow()
        row("NSN") = C1NSNA.Text
        dtMaster.Rows.Add(row)
    End Sub
Your approach is not right. Look:

1. First, you obtain your data from your source (I guess a database).
2. If you're going to bind this data to a list, once the binding is done then it's not possible to alter the items in the list. The list is "binded" to the source. So, if you want some items to appear that are not in your source (database), then manually add them to the datatable rows before doing the binding. So you cannot add items in the click of a button, because the binding has been done before.

Hope that helps.
I am using xml as a datasource. The data is loaded to the list box with following code:
          Dim _table As DataView = dtMaster.DefaultView
            _table.RowFilter = "NSN_ID in (" & keyNsn & ")"
            C1NSN.DataSource = _table
            C1NSN.DataTextField = "NSN"
            C1NSN.DataValueField = "NSN_ID"
            C1NSN.DataBind()

In click event of Add button if I have the following code to add a new row to dtMaster

'Add data to dtMaster datatable
  Dim row As DataRow = dtMaster.NewRow()
   row("NSN") = C1NSNA.Text

Ho do you rebind dtMaster to the listbox (C1NSN)?

'
ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK. I will try it and get back to you later.

Below is the code I am using with the listbox to save data from the listbox, but i think it might be simpler to use a similar approach with a Grid View to achieve the same goal. The Gridview was not used in the web version because it was not possible to add new rows, but I think for the Windows version, it might be a better approach. Thanks for your Help.

 For x As Integer = 0 To C1NSN.Items.Count - 1
                If CheckForNSNFix.Contains(C1NSN.Items(x).ToString) Then
                    For Each xnLink As Xml.XmlNode In xdNSN.SelectNodes("/Root/NSNTable[NSN='" & C1NSN.Items(x).ToString & "']")
                        NSN_ID = xnLink.SelectSingleNode("NSN_ID").InnerText
                        If NSN_ID <> "" Then
                            CD = NSN_ID
                            If u > 0 Then
                                num1 = num1 & "" & CD & ","
                            Else
                                num1 = CD & ","
                            End If
                            u = u + 1
                        End If
                    Next
                End If
            Next

            'Assign ID to data not already in XML file
            For x As Integer = 0 To C1NSN.Items.Count - 1
                If Not CheckForNSNFix.Contains(C1NSN.Items(x).ToString) Then
                    Dim xNew As XElement = New XElement(xtable)
                    xNew.Add(New XElement(xid, num))
                    xNew.Add(New XElement(curXYZ, C1NSN.Items(x)))
                    MyFix.Root.Add(xNew)
                    MyFix.Save((Application.StartupPath + "\App_Data\" & curXYZ & ".xml"))
                    If uu > 0 Then
                        num2 += num & ","
                    Else
                        num2 = num & ","
                    End If
                    uu = uu + 1
                    num = num + 1
                End If

            Next
            searchIdNSN = num1 & num2
Thank You!