Solved

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

Posted on 2004-08-20
9
967 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 65 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 60 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now