Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i am facing a problem i can not solve i searched a lot but it doesn't work, i using mvvm i successfully loaded all the employee details in my datagrid and i have a combobox loading it with gender to filter the datagrid rows with the gender but datagrid does not update to show the new list , i used Inotifychangedproperty but it is useless i showing my code to advice me

What I have tried:

C#
public EditaEmployeesViewModel()
{



	SelectionChangedCommand = new RelayCommand(cbGenderSelectedChanged);

	_EmployeeList = new ObservableCollection<Model.Employees> 
	(DataAccess.EmplyeeDatabaseLayer.GetEmployeeFromDataBase());

	_GenderList = new ObservableCollection<Model.Gender> 
	(DataAccess.GenderDataBaseLayer.GetGenderFromDataBase());
}

ObservableCollection<Model.Employees> _EmployeeList;
ObservableCollection<Model.Gender> _GenderList;

public RelayCommand SelectionChangedCommand { get; set; }

void cbGenderSelectedChanged(object parameter)
{
	_EmployeeList = new ObservableCollection<model.employees> 
	(DataAccess.EmplyeeDatabaseLayer.GetEmployeesWithGenderID(SelectedGenderID));
}

public ObservableCollection<model.employees> EmployeeList
{
	get
	{
		return _EmployeeList;
	}
	set
	{
		_EmployeeList = value;
		Notifypropertychange("EmployeeList");
	}
}

public ObservableCollection<model.gender> GenderList
{
	get
	{
		return _GenderList;
	}
	set
	{
		_GenderList = value;
		Notifypropertychange("GenderList");
	}
}

public event PropertyChangedEventHandler PropertyChanged;

private void Notifypropertychange(string propertyName)
{
	if (PropertyChanged != null)
	{
		PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
	}
}

here is my combobox and datagrid
XML
<ComboBox ItemsSource="{Binding GenderList,UpdateSourceTrigger=Explicit}" 
          DisplayMemberPath="GenderName" SelectedValuePath="ID"
          Grid.Column="1" Name="cbGender"  SelectedValue="{Binding 
          SelectedGenderID}"  IsSynchronizedWithCurrentItem="True" >
			<i:Interaction.Triggers>
				<i:EventTrigger EventName="SelectionChanged">
					<i:InvokeCommandAction  Command="{Binding 
					 SelectionChangedCommand}"/>
				</i:EventTrigger>
			</i:Interaction.Triggers>
		</ComboBox>

<DataGrid Grid.Row="2" AutoGenerateColumns="False" CanUserAddRows="False" 
          SelectedItem="{Binding SelectedEmployee}"
          ItemsSource="{Binding EmployeeList,UpdateSourceTrigger=PropertyChanged}"
          SelectionMode="Single" SelectedIndex="{Binding SelectedIndex}"
		  IsSynchronizedWithCurrentItem="True">
Posted
Updated 13-Dec-18 20:05pm
v4

1 solution

You're breaking Data Binding rules when you select an item in your ComboBox.
C#
void cbGenderSelectedChanged(object parameter)
{
    _EmployeeList = new ObservableCollection<model.employees> 
    (DataAccess.EmplyeeDatabaseLayer.GetEmployeesWithGenderID(SelectedGenderID));
}

If you have a collection object bound to the UI via Data Binding, Recreating the collection will break the binding.

So to fix, you need to do something like:
C#
void cbGenderSelectedChanged(object parameter)
{
    _EmployeeList.Clear();
    for each (var item in DataAccess.EmplyeeDatabaseLayer
                                    .GetEmployeesWithGenderID(SelectedGenderID))
    {
        _EmployeeList.add(item);
    }
}
 
Share this answer
 
Comments
Ramy82 14-Dec-18 2:48am    
thx for your good advice
Ramy82 14-Dec-18 3:33am    
can i ask anther question , if i want to load the form with selected gender , how to till the EmployeeList that the seleced gender is somthing

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