Implementing master-detail with 2 tables in a dataset (wpf) - wpf

I've created a dataset that contains 2 tables:
Users (userID, UserName, other user details)
Emails (Id, UserId, UserEmail)
I populate the dataset using 2 DataAdapters (one for each table)
I have a listbox, a few textboxes and a grid.
listbox gets all the users, the few textboxs displays the user details when picked in the list box (this is easy b/c they are both bound to the same table).
the grid should display the selected user's email addresses.
How do I do it using binding ? is it possible or should I catch the selection change event and filter the grid "manually" (currently the grid displays all the emails in the tables).

If you have a collection that contains all the emails, then you could try looking at making the ItemsSource of your grid an object that implements the ICollectionView interface - this allows you to filter, sort and group your collection...
You can get an object that implements this interface by calling
var view = CollectionViewSource.GetDefaultView(myList);

You can have a property called SelectedUser and bind it to the SelectedItem of ListBox. In the setter of the property you can filter the email list that is bound to grid.
But in the long run, you can create models out of your tables using some ORM tools or Linq-to-sql available in VS which will create models and their relationship. Hence when you will have something like this
Class User
{
UserId, UserName, List<Email> that user has
}
You can create a List<User> and a SelectedUser property which would be bound to the UI elements.
The grid would be bound to SelectedUser.Emails Hence everything is bound and the flow would work fine.

Related

Why do all grid views get updated on adding item from common list control in active gridview in wpf?

I have a bind object of observable collection to my data grid view. But I am not able to figure out where issue exactly occurs. As I bind collection objects to grid views and when I add an item from list control in active grid view (Grid present in one tab), at the same time all grid views get reflected with newly added record in all tabs.
i have created 2 view models and 2 user controls. 1st (common view) user control contains ComboBox control.
2nd view contains Gridview.
View2.cs=
View2ViewModel _vIewModel= new View2ViewModel(EventAggrtorService.Instance.EventAggregator, _vIewModel,DataCollection);
this.mydtGrid.ItemsSource = _vIewModel.DataCollection;
public ObservableCollection DataCollection = new ObservableCollection();
In 1st view model i have publish event to send selected item from combobox control and 2nd view model have subscriber.logic to insert data in gridview.
Problem occurs when i click on ribbon button (it create new window and executes 2ndViewModel constructor) for new tab to open then that new window got subscribed for event i publish from view 1. hence all views calls logic to InsertData i selected from combo control.hence all views shows new record.
public 2ndViewModel(IEventAggregator evAggrtor ,ObservableCollection DataCollection)
{
this._eventAggregator = evAggrtor;
this.DataCollection =DataCollection;
SubscriptionToken token = _eventAggregator.GetEvent<SelectedItemEvent2>().Subscribe(FillLookUpObject,
Microsoft.Practices.Composite.Presentation.Events.ThreadOption.PublisherThread);
}

Two ListViews with different filters on the same data set

I have a ViewModel with some ObservableCollection _questions, which is loaded from DB when VM instance created. Also this list is used to collect data to save back to DB.
This Vm is used for a View1 and it displays the list in ListView with filtering by a property using CollectionViewSource.GetDefaultView(_questions).Filter = ...
Now I need to create View2 which will display same list but without filtering.
I can't bind it to the same ObservableCollection _questions because it has filter defined on CollectionViewSource, but I need to use it to keep SaveToDb code same.
Is it possible to have different filtering on the same data source for two different ListViews?
I have never enjoyed using CollectionViewSource. I would instead filter using a new property in my ViewModel that filters using Linq:
public IEnumerable<MyType> FilteredItems()
{
get
{
return MyCollection.Where(x => x.MyProperty == SomeValue).ToArray;
}
}
I would then bind ItemsSource to this property and use INotifyPropertyChanged event to notify UI of changes to the collection.
Its hard to tell if this fits your scenario well as there is not much information provided on what you need to achieve.

WPF MVVM : get a collection of cell-data corresponds to a column

Is there any way to get DataGridColumns cells-data as a collection of cell-data corresponds to this column?
Note that I'm using MVVM and my datagrid is being dynamically built by DataGridColumn collection!
Thanks!
If you are really using MVVM, then you will know that you should have all of the data that is displayed in the view in your related view model. If that is correct, then you will have a collection that is data bound to the DataGrid.ItemsSource property. As we work with data in WPF and not UI elements, then you can get a collection that contains all of the values from one column using LinQ.
Let's say that you have a column (and therefore a property of your data type) that you want to single out. Let's say that that property is a string and named Name. You can gather all of the values of that property from each item in the collection like this:
List<string> names = yourCollection.Select(i => i.Name).ToList();
If it were an int property named Age, you could do this... and so on:
List<int> ages = yourCollection.Select(i => i.Age).ToList();

How do i do master detail implementation with WPF MVVM and Entity framework

I have four entity Customer, product, order and order details. On my WPF window I have customer list box showing customer name and on select of customer I would like to populate the order list box which also in my window.
Since i am using MVVM, I should have two view model, one for customer and another for order, right? and i should pass the customer to orderview model so that it can populate the orderview.
How do I even pass selected customer to the order view model? I have a property named selected item on customer., but I still dont c how shall I get that in my order view model.
Update:
#Craig Trombly I have created the ObservableCollection of my order entity and has property on my view model that is binding to in Xaml. I am implementing master detail behavior like on select of customer populate my order list box. For that i need to have a property on my customer view model and I am binding that property to selectedItem in the list box in my customerView Xaml. i named that property as selectedItem. however whenever i am trying to access that selected item property from orderview model. it is not working. can you plz tell what i am doing wrong? that selectedItem property in my customer view model should set everytime I select a new item in the list on customer view. it s not doing that either.
The ViewModel is tied to your View (the xaml & cs), it is not around the data.
For instance, MainWindow.xaml & cs should have a MainWindowViewModel.cs
You use one ViewModel to your view. I would suggest using the entity framework for your data.

Getting data from Listview WPF

I have 2 listviews that serve different purposes. Short question is that I need to find out how to pull specific columns from a WPF listview to add them to properties of an object.
Explanation of what i'm doing:
Listview 1:
Bound to a database table. A user changes a combo box in order to filter the table that the listview is bound to. - I do not need help with this.
Listview 2:
This listview is bound to an observable collection with 3 properties.
- I do not need help with this.
User action:
The user selects a subset of items from Listview 1 and clicks "add". I want to add specific columns of listview 1 to the properties of an "employee" object and then added to an observable collection so they can be displayed in Listview 2.
What I have completed:
The databinding of listview 1 and listview 2 work perfectly. I have an employee class with 3 properties (agent id, name, office). I created an observable collection that I will be adding the employees to - IM FINE with this part.
What I need:
I need to know how to find the specific data of listview 1 in order to assign the correct pieces to the corresponding properties of the objects in my observable collection.
My attempt is really an epic fail.. I will loop through all selected items to get the data from each, but for my try I only used the first selected item:
Class windEmployee
Private Agents As New ObservableCollection(Of Employee)
Private sub AgentData()
Dim x As DataRowView
X = Listview1.SelectedItems(0)
Agents.Add(New Employee With {.AgentID = x.Row.Item(9), .Name = x.Row.Item(6) & " " & x.Row.Item(7), .Office = x.Row.Item(16)}
end sub
End Class
DataRowViewHave you tried just itterating through the SelectedItems?
foreach (DataRowView row in Listview1.SelectedItems)
{
...
}

Resources