Link to home
Start Free TrialLog in
Avatar of Anthony Matovu
Anthony MatovuFlag for Uganda

asked on

why is my the selectedindex of a combobox automatically starts at zero

my code is below

       If s = "ALL" Then
            pstr = "select * from [villageUnit];"
        Else
            pstr = "select * from [villageUnit] where [country] = '" & s & "';"
        End If
     
        Dim dbadp As New OleDb.OleDbDataAdapter(pstr, db)
        Dim dTable As New DataTable
        dbadp.Fill(dTable)
        dbadp.Dispose()

        vunits.DataSource = dTable
        vunits.DisplayMember = "villageName"
        vunits.ValueMember = "vid"
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Indices are 0 based in .NET so first item is at index 0. Selected index will be -1 if no item is selected.
Avatar of Anthony Matovu

ASKER

it changes from -1 to 0 when it reads this row
vunits.DataSource = dTable
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland 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
SOLUTION
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
I have found this from the net and seem to be very close to what i want. i trying to understand the solution provided

In a non-databound combo box, nothing is selected unless you explicitly set the SelectedIndex or SelectedItem property. However, when you use data binding, the default behaviour is for the first item to be selected. It can be very difficult trying to figure out how to select nothing. Setting the SelectedItem to null has no effect visually. Setting the SelectedIndex to -1 can cause unpredictable behaviour if the form is an MDI child, or if it is on a tab control and you flip between the tab pages.

The solution to both problems is to give the combo box a different binding context to the form and tab control, bind the data, and then set the SelectedIndex to -1. Like so:

[C# .NET] this.companyComboBox.BindingContext = new BindingContext(); // Bind the data this.companyComboBox.SelectedIndex = -
ASKER CERTIFIED SOLUTION
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
Thank you JameBurger

Now I have a challenge like below

This is the code for updating the combobox (smonth) for month.

smonth.Items.Clear()
        smonth.Items.Add((New cValue(" ", 0)))
        smonth.Items.Add((New cValue("January", 1)))
        smonth.Items.Add((New cValue("February", 2)))
        smonth.Items.Add((New cValue("March", 3)))

It is fine to read the selected value and I can save properly

Challenge comes when i try to read the saved value from the database and display its test on the combobox. The code below is what i use to display


 If drd(g).ToString.Trim.Length > 0 Then CType(frm.Controls(drd.GetName(g)), ComboBox).SelectedValue = CType(drd(g), Long)

Anthony