YZlat
asked on
ComboBox question
I have a ComboBox that gets filled from the database:
cbTest.DataSource = ds.Tables(0).DefaultView
cbTest.DisplayMember = "name"
cbTest.ValueMember = "ID"
When form is loaded, the first customer returned by the query is displayed in the combo box.
I want to display a text "Select" instead.
I tried cbText.Items.Add("Select")
and cbText.Items.Insert(0,"Sel ect") but nothing works
cbTest.DataSource = ds.Tables(0).DefaultView
cbTest.DisplayMember = "name"
cbTest.ValueMember = "ID"
When form is loaded, the first customer returned by the query is displayed in the combo box.
I want to display a text "Select" instead.
I tried cbText.Items.Add("Select")
and cbText.Items.Insert(0,"Sel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
also
NewDR(2) = "Select..."
should be
NewDR(1) = "Select..."
and I get invalid index error here:
'Initially show blank row in ComboBox.
ddlCompName.SelectedIndex = (dsTemp.Tables(0).Rows.Cou nt) - 1
NewDR(2) = "Select..."
should be
NewDR(1) = "Select..."
and I get invalid index error here:
'Initially show blank row in ComboBox.
ddlCompName.SelectedIndex = (dsTemp.Tables(0).Rows.Cou
ASKER
Ok, I've changed your code around a little and it worked:
Dim NewDR As DataRow
NewDR = ds.Tables(0).NewRow
NewDR(1) = "Select One"
ds.Tables(0).Rows.InsertAt (NewDR, 0)
If ds.Tables(0).Rows.Count > 0 Then
cbTest.DataSource = ds.Tables(0).DefaultView
End If
cbTest.DisplayMember = "name"
cbTest.ValueMember = "ID"
Dim NewDR As DataRow
NewDR = ds.Tables(0).NewRow
NewDR(1) = "Select One"
ds.Tables(0).Rows.InsertAt
If ds.Tables(0).Rows.Count > 0 Then
cbTest.DataSource = ds.Tables(0).DefaultView
End If
cbTest.DisplayMember = "name"
cbTest.ValueMember = "ID"
I said that my code was for a web application so the syntax for you would be a little different. =P
ASKER
In VB.NET there is no DataBind method