Reputation: 800
I query some data from multiple databases and save them in a two-dimensional string array like this:
string[databaseID,fieldID]
... where fieldID = 0 represents the Name of the field.
The number of databases varies and can be between at least one and an undefined number. The number of fields is determined.
I want to have one column for the field names and one column for each databaseID. Each row should contain the data of a specific field of each database. It should for example look something like this:
FieldName | Database1 | Database2 | Database3
----------|-----------|-----------|-----------
Name1 | A | B | A
----------|-----------|-----------|-----------
Name2 | 1 | 2 | 3
How can I do this?
For creating the column headers, I already have this:
GridView gridView = new GridView();
gridView.AllowsColumnReorder = true;
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("");
gvc1.Header = "Feldname";
gvc1.Width = 100;
gridView.Columns.Add(gvc1);
for (int i = 0; i < databases.Count; i++)
{
GridViewColumn gvc = new GridViewColumn();
gvc.DisplayMemberBinding = new Binding("Path=Row[" + (i + 1) + "]"); //Still not sure, wether this is okay
gvc.Header = databases[i].DisplayName;
gvc.Width = 100;
gridView.Columns.Add(gvc);
}
lvBasic.View = gridView;
The XAML part I refer to is rather simple:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ListView Name="lvBasic">
</ListView>
</ScrollViewer>
UPDATE: I thought it wouldn't matter, so I left out an aspect of my problem. I need to compare the data of database1 with all other databases and therefore need this layout or some other layout which is suitable for that task.
Upvotes: 0
Views: 1382
Reputation: 7456
This is an approach to explain my comment.
XAML
<Grid >
<Grid.Resources>
<CollectionViewSource Source="{Binding DataObjects}" x:Key="CollectionViewSource" >
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="DatabaseId"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListView ItemsSource="{Binding Source={StaticResource CollectionViewSource}}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FieldId}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
DataObject
public class DataObject {
public string DatabaseId { get; set; }
public string FieldId {
get; set;
}
}
Usage
this.DataObjects = new List<DataObject>();
this.DataObjects.Add(new DataObject {DatabaseId = "Db1", FieldId = "FieldName"});
this.DataObjects.Add(new DataObject { DatabaseId = "Db1", FieldId = "FieldFirstName" });
this.DataObjects.Add(new DataObject { DatabaseId = "Db2", FieldId = "FieldName" });
this.DataObjects.Add(new DataObject { DatabaseId = "Db2", FieldId = "FieldDate" });
this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldName" });
this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldDate" });
this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldFirstName" });
this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldId" });
This is an 100% MVVM-Conform solution. You can just simply group and expand by your Database (or just reverse the logic if that fits your needs better).
Please note, that my DataObject
has nothing in common with your actual datastructure. I made this in about 7 minutes and it should give you an alternate view.
Cheers
Upvotes: 1