Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the name of God
hi

I want to filter item of a combo box with another combo box

for example
One of the combobox containing shoes and other including designs when choosing a shoe the designs which related to this shoe come in second combobox

i want use lambda expression in select index change but i dont know how
Posted
Comments
Debabrata_Das 13-Jun-14 6:31am    
That is fine but you have to try yourself first. If you are stuck somewhere, people can help you out.
- DD

1 solution

C#
private void ComboxwithLambad_Load(object sender, EventArgs e)
      {

          LoadCountries();
      }

      public class Country
      {
          public int CountryId { get; set; }
          public string CountryName { get; set; }
      }

      public class Cities
      {
          public int CityId { get; set; }
          public string CityName { get; set; }
          public int CountryId { get; set; }
      }


      private List<cities> GetAllCities()
      {
          List<cities> lstCities = new List<cities>()
          {
              new Cities(){ CityId=1, CityName="Banglore", CountryId=1},
              new Cities(){ CityId=2, CityName="Islambad",CountryId=2},
              new Cities(){ CityId=3, CityName ="Newyork",CountryId=3},
              new Cities(){ CityId=4, CityName="Chennai",CountryId=1},
              new Cities(){ CityId=5, CityName="New Delhi", CountryId=1},

              new Cities(){ CityId=6, CityName ="Peru",CountryId=2},
              new Cities(){ CityId=7, CityName="Washington D.C.",CountryId=3},
              new Cities(){ CityId=8, CityName="Trivandrum", CountryId=1},
              new Cities(){ CityId=9, CityName="Peshawar", CountryId=2},
                new Cities(){ CityId=9, CityName="Kohat", CountryId=2}

          };

          return lstCities;
      }
      private void LoadCountries()
      {
          List<country> lstCountry = new List<country>()
          {
             new Country{ CountryId =1, CountryName="India"},
             new Country{ CountryId =2, CountryName="Pakisthan"},
             new Country{ CountryId =3, CountryName="USA"}
           };

          cbCountry.DataSource = lstCountry.ToList();
          cbCountry.DisplayMember = "CountryName";
          cbCountry.ValueMember = "CountryId";


      }

      private void cbCountry_SelectedIndexChanged(object sender, EventArgs e)
      {
      }

      private void cbCountry_SelectionChangeCommitted(object sender, EventArgs e)
      {



          List<cities> allCities = new List<cities>();
          allCities = GetAllCities();

          if (cbCountry.Items.Count > 0)
          {
var citiesByCountry = allCities.Where(r => r.CountryId ==Convert.ToInt32(cbCountry.SelectedValue)).ToList();

              cbCities.DataSource = citiesByCountry;
              cbCities.DisplayMember = "CityName";
              cbCities.ValueMember = "CityId";
          }
      }
 
Share this answer
 
v2
Comments
roza54 14-Jun-14 3:47am    
thank you very much-it was very good solution
Prasad Avunoori 15-Jun-14 23:29pm    
You are most welcome Roza.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900