Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add a new ITEM to a COMBOBOX with datasource?

Status
Not open for further replies.

VladimirKim

Programmer
Jan 20, 2005
49
US
Hi,
I have this code:

Public HISetID As Integer
public RTID as Integer

Private Sub frmHotelInventoryManager_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

cmbHotelInventorySet.ValueMember = "HotInvSetID"
cmbHotelInventorySet.DisplayMember = "HotInvSetDesc"
Me.cmbHotelInventorySet.DataSource = GetHotelInventorySets(RTID).Tables(0)

End Sub

Now, I would like to add a new item that says "ALL" and the value for that item should be 0, since the values in the table in the datasource start from 1. How would I do that? thanks in advance.
 
Can't you just use the Insert method e.g.
Code:
cmbHotelInventorySet.Items.Insert(0, "Test")

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Nope,
here is the message that I'm getting:

System.ArgumentException: Cannot modify the Items collection when the DataSource property is set
 
just add a row to the datasource

Code:
dim r as datarow
r = GetHotelInventorySets(RTID).Tables(0).newrow
r.item(0) = ...
r.item(1) = ...


Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
don't forget to add the row once your're done setting it ;)

Code:
dim r as datarow
r = GetHotelInventorySets(RTID).Tables(0).newrow
r.item(0) = ...
r.item(1) = ...
GetHotelInventorySets(RTID).Tables(0).rows.add(r)

-rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
ok, I've done this:

Dim r As DataRow
Me.cmbHotelInventorySet.DataSource = GetHotelInventorySets(RTID).Tables(0)
r = GetHotelInventorySets(RTID).Tables(0).NewRow
r.Item(0) = 0
r.Item(1) = "ALL"
GetHotelInventorySets(RTID).Tables(0).Rows.Add(r)


But, now I'm getting this error:
System.ArgumentException: This row already belongs to another table.
 
Apologies for my suggestion - I really should read the question more closely before replying!

Anyway, here's an example of how to use InsertAt to add a new record:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Declarations
        Dim dt As New DataTable
        Dim dr As DataRow

        ' Create a datatable with ten sample records
        dt.Columns.Add("Item1")
        For i As Integer = 1 To 10
            dr = dt.NewRow
            dr(0) = i
            dt.Rows.Add(dr)
        Next

        ' Add the "ALL" record to the datatable
        dr = dt.NewRow
        dr(0) = "ALL"
        dt.Rows.InsertAt(dr, 0)

        ' Bind the ComboBox to the DataTable
        ComboBox1.ValueMember = "Item1"
        ComboBox1.DisplayMember = "Item1"
        ComboBox1.DataSource = dt
    End Sub


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top