Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox with empty first value

Status
Not open for further replies.

heseir

IS-IT--Management
Aug 15, 2005
5
NO
Hallo,

I have this code today:
dv = new DataView(ds.Tables[0]);
cbo.DataSource = dv;
cbo.DisplayMember = "DESCRIPTION";
cbo.ValueMember = "ID";

This gives me a combobox / DropDownList which always choose a value. Since the combobox is optional, this needs to be changed, so that the default top value always is " " (empty). I tried adding SelectedIndex = -1, and this gives the wanted result, but if user by accident choose a value, and then want to go back, the is no " " (empty) value to choose.

Can this be achieved without adding an empty value to my database??

Thx in advance!



Best regards,
Eirik Hesthamar
Norway
 
Nevermind, solved it.

If anyone is interested, I solved it using this code:
DataRow drNew = null;
drNew = ds.Tables[0].NewRow();
drNew["ID"] = 0;
drNew["DESCRIPTION"] = "";
drNew.EndEdit();
ds.Tables[0].Rows.InsertAt(drNew, 0);

Best regards,
Eirik Hesthamar
Norway
 
Dear Heseir,
I was actually working on it right now, for me i always use the old way in filling a combobox by loopin through it and doing it all manually. So i just took time to practice ur method.Thank you for posting the solution.
I wonder what's the advantage of assigning a datable to a combobox over using the looping method. Knowing that the looping method gives me more control on the control like:
1. displaying concatinated strings
2. adding or removing items freely without affecting the datatable.
3. ability to clear it without having a DB trip.

Thank you for your time :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top