Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a listview and two columns Country and state (both are combo box).i want to filter state combo box with country combo box .i didnt get object of combo State in comboCountry_SelectionChanged Event. help me
Posted
Updated 3-Dec-15 23:14pm
v3
Comments
Anisuzzaman Sumon 4-Dec-15 2:36am    
Please Show some code
Where is the issue?
Mahesh Alappuzha 4-Dec-15 5:08am    
i didnt get object of combo State in comboCountry_SelectionChanged Event.

This is probably easiest done by creating a DataSet with two DataTable, Countries and States, with a relation in between.

Then you use two BindingSource, one for Countries and one for States, that you use as DataSource for your combo boxes.

The code below is written for a Windows Form with two regular ComboBox controls, but the principal is the same for your case.
Only the last part of the code where the combo boxes are assigned the data sources should differ.
C#
DataSet dsData = new DataSet("CountriesAndStates");

DataTable dtCountries = dsData.Tables.Add("Countries");
DataColumn dcPK = dtCountries.Columns.Add("CountryID", typeof(int));
dtCountries.Columns.Add("Name", typeof(string));

dcPK.AutoIncrement = true;
dtCountries.PrimaryKey = new DataColumn[] { dcPK };

DataTable dtStates = dsData.Tables.Add("States");
dtStates.Columns.Add("Name", typeof(string));
DataColumn dcFK = dtStates.Columns.Add("CountryID", typeof(int));

dsData.Relations.Add("Country_State", dcPK, dcFK);

DataRow drParent = null;

drParent = dtCountries.NewRow();
drParent["Name"] = "USA";
dtCountries.Rows.Add(drParent);

dtStates.Rows.Add("Alabama", drParent["CountryID"]);
dtStates.Rows.Add("Alaska", drParent["CountryID"]);
dtStates.Rows.Add("Utah", drParent["CountryID"]);

drParent = dtCountries.NewRow();
drParent["Name"] = "Canada";
dtCountries.Rows.Add(drParent);

dtStates.Rows.Add("Alberta", drParent["CountryID"]);
dtStates.Rows.Add("British Columbia", drParent["CountryID"]);
dtStates.Rows.Add("Quebec", drParent["CountryID"]);

BindingSource bsCountries = new BindingSource(dsData, "Countries");
BindingSource bsStates = new BindingSource(bsCountries, "Country_State");

cbCountries.DataSource = bsCountries;
cbCountries.DisplayMember = "Name";

cbStates.DataSource = bsStates;
cbStates.DisplayMember = "Name";


I tried to do an implementation of this in WPF as well, but being a total newbie to this technology I found it a bit difficult.
In other words, I failed miserably. :-(
And no more time today.
 
Share this answer
 
v3
C#
private System.Windows.DependencyObject FindChildControl<T> ( System.Windows.DependencyObject control , string ctrlName , long lRowid )
      {
          int childNumber = System.Windows.Media.VisualTreeHelper.GetChildrenCount ( control );
          for ( int i = 0 ; i < childNumber ; i++ )
          {
              System.Windows.DependencyObject child = System.Windows.Media.VisualTreeHelper.GetChild ( control , i );
              System.Windows.FrameworkElement fe = child as System.Windows.FrameworkElement;
              // Not a framework element or is null
              if ( fe == null ) return null;

              if ( child is T && fe.Name == ctrlName && fe.Tag.ToString ( ) == lRowid.ToString ( ) )
              {
                  // Found the control so return
                  return child;
              }
              else
              {
                  // Not found it - search children
                  System.Windows.DependencyObject nextLevel = FindChildControl<T> ( child , ctrlName , lRowid );
                  if ( nextLevel != null )
                      return nextLevel;
              }
          }
          return null;
      }
 
Share this answer
 

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