0

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.

6
  • 1
    Wouldnt it be much easier, to create a new object, containing the Array-Data and simply group them? That might increase the readability too Commented Aug 3, 2016 at 11:38
  • What do you mean by that? Commented Aug 3, 2016 at 11:50
  • The thing you're looking for is a PivotGrid. Unfortunately this is not built in but there are many companies offering such possibilities in their frameworks (Telerik, Infragistics, DevExpress, etc). Needless to say, you wont get em for free :( Commented Aug 4, 2016 at 7:24
  • @lokusking So, is there an hack or a workaround to get what I want? It doesn't have to be pretty. :) Commented Aug 4, 2016 at 9:02
  • 1
    Maybe something like this if you have a similar Data-Structure Commented Aug 4, 2016 at 12:57

1 Answer 1

1

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" });

Result resultImage

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's actually not bad. The problem is, that I left out an aspect in my explanation to simplify it. I want to compare some data of a specific database with all other databases. Therefore I'd like to have it in that column-style-view. Database1 is kind of a master database I'd like to compare all data against.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.