binding a datatable to a combobox

  • 12 years ago

     Hi guys,

    I'm trying to bind a datatable to a combobox and I stumbled upon an error I don't seem to figure out. Here is my code:

     

            Dim dt As New DataTable("DatabaseTypes")
            dt.Columns.Add("Long Name")
            dt.Columns.Add("Code")

            dt.Rows.Add("SQL Server", "sqlserver")
            dt.Rows.Add("Oracle", "oracle")


            cmbODBCTemplate.Items.Clear()

            cmbODBCTemplate.DataSource = dt
            cmbODBCTemplate.DisplayMember = dt.Columns(0).ColumnName
            cmbODBCTemplate.ValueMember = dt.Columns(1).ColumnName

     The error that I'm getting is: "Index was outside the bounds of the array." and seems to happen when executing

           cmbODBCTemplate.DataSource = dt

    Any ideas will be truly appreciated.

    Thanks,

    Bogdan

     

     

  • 12 years ago

    That snippet of code works for me and the combobox fills with the 2 database names.

    Is there anything else that you've omitted in the code that you've posted above?

     

  • 12 years ago

    Oh, on the second look, it seems the code I have in the combo's SelectedValueChanged event causes the problem. I just discovered this while I was trying to give you more details. Smiley Face

    Thanks,

    Bogdan

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”