Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1285
  • Last Modified:

Set a databound Combobox to a null-value or selectedindex=-1

Hi folks,

i´m not sure i can do this with my databound combobox, but let´s give it a try. I´m trying to build a search form, that has n databound comboboxes. My users should be able to only select the comboboxes they want to be base of their search. My idea ist simple, i did it thousand times in MS Access. Only if a value in a combobox is selected, my Search-Button adds the parameter to my needed sql-statement. To my big surprise there´s no possibility to tell the combobox-control that it should display nothing. It´s always displaying the first entry. Argh. I want to initialize the comboboxes like that:

cmb_Box1.SelectedValue = System.DBNull; (kinda, only for example)
or
cmb_Box1.SelectedIndex=-1 (how it´s mentioned in the vs.net docu, but that doesn´t work either)

Anyone a good idea? All i want is a databound combobox that has initially no value and displays nothing. HELP! :-)

Regards, Stefan
0
surlberg
Asked:
surlberg
2 Solutions
 
chmohanCommented:
you could try one method
dont load the combobox until the user clicks on it
0
 
EBatistaCommented:
could be somebody with a better idea, but the way i address that is placing one checkbox per combobox, initially with all the combo disable, so when the user check a checkbox the corresponding combo becomes enable indicating that he/she wants to use that field as criteria and just then its data is loaded.
That way i am not pulling out data from the datasource that the user is not going to use, and i build the querystring dinamically using the StringBuilder class based in the checkboxes status.

hope that help
0
 
dunglaCommented:
on my way, after bounding data to combo box, i insert null value in to the first index of list such as:

cmb_Box1.DataSource = DataSource;
cmb_Box1.DataBind();
cmb_Box1.Items.Insert(0, new ListItem("",""));

or event you can handle Init event of combo box and on init, try to add the null value to first index of your combo box.

That my used solution.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
sumixCommented:
     there is a bug (of course, not the only one) in combobox that does not let you properly display a null value. When you set SelectedIndex = -1 and the previously selected item was the first one (SelectedIndex=0) you will get a null value. Otherwise the combobox will be set to the first item and then you have to set again SelectedIndex to -1. So just try something like
   cmb_box1.SelectedIndex = -1;
   cmb_box1.SelectedIndex = -1;

 :)
0
 
EBatistaCommented:
dungla, what you say is ok for dropdowns asp control, but that dont work for winforms comboxbox, it will be great if it does :(
0
 
dunglaCommented:
EBatista, i try both in web and winforms. What's happend?
0
 
surlbergAuthor Commented:
Hey Guys,

i´ve tried the SelectedIndex=-1 x 2, no effect. The idea of dungla sounds great, but in WinForms there doesn´t seem to exist a new ListItem. For the moment i tried the checkbox-Version of EBatista. The way of filling the combobox the moment the user clicks in it sounds okay, but there is no way to get it unselected, if once loaded. Okay, i could use a doubleclick-event or a button to clear the datasource and reload it when the user clicks on it, but hell, is that the top of the evolution microsoft has to offer? The version with checkboxes uses the "same" technique, but offers the user a more simple way of understanding in my opinion.

.NET is really okay, but it has some very disturbing problems i´m slowly getting mad with. All i want (and i can´t be the only one) is a simple null value displayed and selected in a combobox... :-)
0
 
123654789987Commented:
Once you have selected a value in the ComboBox u can clear that value by giving

ComboBox.SelectedIndex = 1;

Where the first index corresponds to the empty string "" value.
0
 
EBatistaCommented:
agree with you surlberg, i found my self asking the same but i have addressed that that way.
if the user uncheck the checkbox then you can turn the combo disable and clear its datasource, you could use the CheckedChanged event from the checkboxes:

private void CheckBox1_CheckedChanged(object sender,System.EventArgs e){
        if (CheckBox1.Checked){
            //set the datasource
            combo.DataSource=something;
            combo.Enable=true;
            return;
            }

         //else
         combo.DataSource=null;
         combo.Enable=false;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now