Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have combobox with enum values.
Now when I select any value from the combobox, I want to populate datagrid with records matching the selected value.

It's wpf mvvm project.
And strictly no code behind

What I have tried:

I have tried using selecteditem property for combobox
Posted
Updated 5-Mar-17 17:19pm
Comments
[no name] 5-Mar-17 20:17pm    
"I want to populate datagrid with records matching the selected value", okay so you know what you want, what is your question?
Member 12229842 5-Mar-17 20:26pm    
I am very new to wpf and mvvm both... I went through lot of articles saying I should use selected item property can be used, I tried it's not working... when I change my selection it reflects in combobox but the datagrid not populating, so my question is how do I do that ?
[no name] 5-Mar-17 20:47pm    
You are kidding right? "it's not working" is not any kind of a description of a problem. We can't see your code or read your mind. You take the selection and use that in your query and use the result of your query as the itemsource for your grid. No one can tell you anything more because you have not described a problem, shown us your code, told us what programming language you are using, what data base engine you are using, nothing.
Member 12229842 5-Mar-17 21:01pm    
i am using entity framework
I have interface igetdatarepository which has below task
Task<List<employee>> getasyncamployee();

Task<List<employee>> getasyncemployeewith(into salary)


Then I have a class which implement this inter face
Getdata

Private dbcontext _context = new dncontext();

Public async Task<List<employee>> getasyncamployee()
{

Return await _contex.employee.tolistasync();
}

Public async Task<List<employee>> getasyncamployee(into salary)
{
Return await_context.employee.Include(e => e.Salary == salary).tolistasync;
Member 12229842 5-Mar-17 21:15pm    
Now the view
<combobox itemsource="{binding salarylist}" Selectedvaluepath=value selectedvalue ="{binding employeedata, mode=two way}


And viewmodel


Igetdatarepository _getdatarepo;
Public EmployeeViewModel(igetdatareposotory repo)
{
If(designerproperties.getisdesignmode(new System.Window.dependencyobject())) return;

_Getdatarepo = repo;
}

Private employee _employeedata;
Public employee employeedata
{
Get
{ return _employeedata; }
Set
{
SetProperty(ref _employeedata, value);
}
}


And I am using c#

1 solution

Your ViewModel (VM) needs to expose a property that is bound to the ComboBox SelectedItem or SelectedIndex property so the VM can be notified of the user selection.

Your VM can then run the query and update your ObservableCollection which will automatically be seen by the bound DataGrid and the DataGrid will reflect the new data.
 
Share this answer
 
Comments
Member 12229842 6-Mar-17 0:09am    
Yeah I tried that

I have a property employeedata which I bound to selected item property of combobox

But when I debug
It's showing null
Member 12229842 6-Mar-17 0:17am    
I was doing it all wrong... I understood the point and it worked... thank you

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