开发者

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开发者_C百科 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜