Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-20
9
Medium Priority
?
1,109 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

688 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