vellaima Posted February 3, 2003 Posted February 3, 2003 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 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, " ") Quote
Cywizz Posted February 3, 2003 Posted February 3, 2003 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? Quote Howzit??
vellaima Posted February 3, 2003 Author Posted February 3, 2003 because the user might select the item in the combobox or leave it blank. Quote
*Experts* Nerseus Posted February 3, 2003 *Experts* Posted February 3, 2003 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
vellaima Posted February 3, 2003 Author Posted February 3, 2003 I would be happy if you could give me a sample code. Quote
*Experts* Nerseus Posted February 3, 2003 *Experts* Posted February 3, 2003 For which technique, adding a row to a datatable or using an ArrayList of custom objects? -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Experts* Nerseus Posted February 3, 2003 *Experts* Posted February 3, 2003 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). 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: 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
vellaima Posted February 4, 2003 Author Posted February 4, 2003 Thanks for providing the sample code. It worked perfectly well. Quote
*Experts* Nerseus Posted February 4, 2003 *Experts* Posted February 4, 2003 whew :) -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.