Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-20
9
Medium Priority
?
1,147 Views
Last Modified: 2008-01-09
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
Comment
Question by:surlberg
9 Comments
 
LVL 1

Expert Comment

by:chmohan
ID: 11854285
you could try one method
dont load the combobox until the user clicks on it
0
 
LVL 10

Expert Comment

by:EBatista
ID: 11855356
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
 
LVL 13

Accepted Solution

by:
dungla earned 195 total points
ID: 11857435
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:sumix
ID: 11858008
     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
 
LVL 10

Expert Comment

by:EBatista
ID: 11866752
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
 
LVL 13

Expert Comment

by:dungla
ID: 11866926
EBatista, i try both in web and winforms. What's happend?
0
 
LVL 1

Author Comment

by:surlberg
ID: 11867537
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
 
LVL 10

Expert Comment

by:123654789987
ID: 11868504
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
 
LVL 10

Assisted Solution

by:EBatista
EBatista earned 180 total points
ID: 11869251
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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question