简体   繁体   中英

Combobox binding and empty item

I am writing a search form for desktop application and I have a problem with combobox binding.
The user can search for properties by various criteria (city, price, etc).
I want to bind combobox to list of all possible cities, but I want to leave the user the option not to choose anything so it can search properties in all cities.
How can I do that?
I'm using linq2sql for data access.

Well, supposing you have bound the combobox to a List of Cities that you pulled from your database:

class City
{
   public string ID{get; private set;}
   public string Name{get;private set;}

   public City(string id, string name)
   {
       ID=id;Name=name;
   }  
}

Just Add an empty city to that list:

cityList.Add(new City("AA",string.empty");

When you bind the combobox to the cityList and sort it, the city with id "AA" will be on top. In your searching code you can now adjust your search algorithm for the case that the user has selected the city with id "AA";

Cleverer solutions are certainly possible, but I would need to see some code of the searching method to help you further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM