MiBlg
asked on
Add "Please Select" in DataBound ComboBox
Hi,
I have a databound combobox which bound to a datatable. I want to put the initial value in my combobox, such as "Please Select Item". However, I've tried setting the Text property and also try to insert a new row without any luck. It seems after the combobox bounded to a datatable, its items can't be changed anyway.
Could someone help me with some tips?
Thanks in advance
MI
I have a databound combobox which bound to a datatable. I want to put the initial value in my combobox, such as "Please Select Item". However, I've tried setting the Text property and also try to insert a new row without any luck. It seems after the combobox bounded to a datatable, its items can't be changed anyway.
Could someone help me with some tips?
Thanks in advance
MI
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what do you mean by the "initialized" this is a common method of inserting data in a datatable
there didn't mension about combobox
when you have the filled datatable object how do you bind the combox to it ?
B..M
there didn't mension about combobox
when you have the filled datatable object how do you bind the combox to it ?
B..M
ASKER
I mean the scenario is like this:
I've populated the combobox using your way:
DataRow dr = MyDataTableNewRow();
dr["Value"] = 0;
dr["Text"] = 'Please Select Item';
MyDataTable.Rows.InserAt(d r, 0)
myCombo.DataSource = myDataTable
myCombo.ValueMember = "Value"
myCombo.DisplayMember = "Text"
When The Form is in Add Mode, the selected item in the combo box is "Please Select Item" (1st item). This works fine.
However, when the Form is in Editing, I have to make myCombo show the value based on the database. For example:
mycombo.SelectedValue = "blabla"
It throws an error when doing this, but when there's no "Please select" it works fine. (I remove the code to insert new row)
What's wrong here?
I've populated the combobox using your way:
DataRow dr = MyDataTableNewRow();
dr["Value"] = 0;
dr["Text"] = 'Please Select Item';
MyDataTable.Rows.InserAt(d
myCombo.DataSource = myDataTable
myCombo.ValueMember = "Value"
myCombo.DisplayMember = "Text"
When The Form is in Add Mode, the selected item in the combo box is "Please Select Item" (1st item). This works fine.
However, when the Form is in Editing, I have to make myCombo show the value based on the database. For example:
mycombo.SelectedValue = "blabla"
It throws an error when doing this, but when there's no "Please select" it works fine. (I remove the code to insert new row)
What's wrong here?
what error do you receive ?
B..M
B..M
ASKER
I Initialize the dr["Value"] to int32.MaxValue, then the error is Index out of range.
ASKER
I still don't know how to figure this out, but anyway you have already pointed me out the way. Thanks
I have the same problem. Seems that setting text to "Select ..." doesn't work.
ASKER
Sorry for the late reply.
I've followed your suggestion but, it's failed when the combox has to be intialized to a value, for example, the 3rd item should be selected.
Your suggestion works perfectly only if the 1st item selected.
Any more ideas?