ComboBox

vellaima

Centurion
Joined
Jan 29, 2003
Messages
109
Hello,

I tried to insert a new value into the combobox which gave me the following error while executing the program.

Error: Can't modify the items collection when the datasource property is set.

Can you please tell me how to rectify and insert a blank value in the combobox

Code:
Dim SQL As String, commission_dataset As DataSet
        Dim commission_adapter As New System.Data.OleDb.OleDbDataAdapter()
        SQL = "Select commission_id,commission_desc from sales_rep_comm"
        commission_adapter = New System.Data.OleDb.OleDbDataAdapter(SQL, OleDbConnection2)
        commission_dataset = New DataSet()
        commission_adapter.Fill(commission_dataset, "sales_rep_comm")

        Commission_based.DataSource() = commission_dataset.Tables("sales_rep_comm").DefaultView
        Commission_based.ValueMember = commission_dataset.Tables("sales_rep_comm").Columns(0).ToString
        Commission_based.DisplayMember = commission_dataset.Tables("sales_rep_comm").Columns(1).ToString

        Commission_based.Items.Insert(0, "  ")
 
Hi Vidhya

As the error message says: You can't modify the items collection when it is bind to a datasource.

Can you maybe clarify why you want to add a dummy entry?
 
Since you're binding directly to the datatable you'll have to add a "dummy" row to your datatable.

As an alternative, you can either build a special object and put it in an ArrayList and bind to that (ask if you want a sample) or create a new DataTable and copy the data from your sales_rep_comm table to the new DataTable. You can then add a row to the new DataTable.

The first option, adding a row to your DataTable, may seem odd at first. But usually a DataTable used to fill a combo is just a lookup table - meaning your app won't be modifying the table in the DataSet (it's only used to populate a combo). Adding a row to a DataTable is easy, too.

-nerseus
 
Here ya go:

First, define a new object type. This is useful if you want to store and ID and a description. If you just need a description you can skip this and just add items directly to an ArrayList (see below).
Visual Basic:
Public Class LookupItem
    Private textData As String
    Private idData As Object

    Public Sub New()
    End Sub


    Public Sub New(ByVal textVal As String)
        Me.idData = 0
        Me.textData = textVal
    End Sub

    Public Sub New(ByVal idVal As Object, ByVal textVal As String)
        Me.idData = idVal
        Me.textData = textVal
    End Sub

    Public ReadOnly Property ID() As Object
        Get
            Return idData
        End Get
    End Property

    Public ReadOnly Property Text() As String
        Get
            Return Me.textData
        End Get
    End Property


    Public Overrides Function ToString() As String
        Return Me.textData
    End Function
End Class

Next, change your binding code to this:
Visual Basic:
        Dim row As DataRow
        Dim al As ArrayList = New ArrayList()
        al.Add(New LookupItem(0, "Blank Value"))
        For Each row In commission_dataset.Tables("sales_rep_comm").Rows
            al.Add(New LookupItem(row(0), row(1)))
        Next
        ComboBox1.DataSource() = al
        ComboBox1.ValueMember = "ID"
        ComboBox1.DisplayMember = "Text"

I hope this works, I'm not too good with VB (I'm a C# guy). The idea is to loop through each row of your DataTable and add the data as a new LookupItem object to an arraylist. Bind the combo to the arraylist instead of the DataTables view. Also, the line right before the For Each adds a dummy row. You may have to change that to use your own values.

-ner
 
Back
Top