Anthony Matovu
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(pst r, db)
Dim dTable As New DataTable
dbadp.Fill(dTable)
dbadp.Dispose()
vunits.DataSource = dTable
vunits.DisplayMember = "villageName"
vunits.ValueMember = "vid"
If s = "ALL" Then
pstr = "select * from [villageUnit];"
Else
pstr = "select * from [villageUnit] where [country] = '" & s & "';"
End If
Dim dbadp As New OleDb.OleDbDataAdapter(pst
Dim dTable As New DataTable
dbadp.Fill(dTable)
dbadp.Dispose()
vunits.DataSource = dTable
vunits.DisplayMember = "villageName"
vunits.ValueMember = "vid"
Indices are 0 based in .NET so first item is at index 0. Selected index will be -1 if no item is selected.
ASKER
it changes from -1 to 0 when it reads this row
vunits.DataSource = dTable
vunits.DataSource = dTable
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Bindi ngContext = new BindingContext(); // Bind the data this.companyComboBox.Selec tedIndex = -
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.Bindi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Lengt h > 0 Then CType(frm.Controls(drd.Get Name(g)), ComboBox).SelectedValue = CType(drd(g), Long)
Anthony
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.Lengt
Anthony