Click here to Skip to main content
11,642,855 members (69,811 online)
Click here to Skip to main content
Add your own
alternative version

Silverlight DataGrid with Dynamic Multiple Row Columns in Header

, 15 Dec 2008 CPOL 71K 3.5K 29
Creating a dynamic header for the Silverlight DataGrid with multiple rows/columns.
DynamicMutipleRowHeaderSilverlighGrid.zip
DynamicMutipleRowHeaderSilverlighGrid
Bin
Debug
de
es
fr
it
ja
ko
zh-Hans
zh-Hant
DynamicMutipleRowHeaderSilverlighGrid.csproj.user
Properties
DynamicMutipleRowHeaderSilverlighGridWeb
App_Data
bin
de
es
fr
it
ja
ko
zh-Hans
zh-Hant
ClientBin
DynamicMutipleRowHeaderSilverlighGrid.xap
DynamicMutipleRowHeaderSilverlighGridWeb.csproj.user
Properties
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace DynamicMutipleRowHeaderSilverlighGrid
{
    public partial class Page : UserControl
    {
        DataGrid grdData = new DataGrid() { AutoGenerateColumns = false };
        bool bGridInitialized = false;
        List<RowHeader> Headers = null;

        public Page()
        {
            InitializeComponent();
            InitUI();
        }

        void InitUI()
        {
            DynamicHeaders dynHead = new DynamicHeaders(SalseData.GetSalseDataHeader());
            Headers = dynHead.ParseHeader();

            grdData.LayoutUpdated += new EventHandler(grdData_LayoutUpdated);

            String[] ColHeaders = SalseData.GetSalseDataColumnHeader().Split(',');
            foreach (String col in ColHeaders)
            {
                String[] colHeader = col.Split('|');
                grdData.Columns.Add(new DataGridTextColumn { Header = colHeader[0], Binding = new System.Windows.Data.Binding(colHeader[1]), IsReadOnly = true });
            }
            grdData.ItemsSource = SalseData.GetSalseData();
            
            for(int i =0; i<Headers.Count+2; i++)
                LayoutRoot.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0, GridUnitType.Auto) });
            
            LayoutRoot.Children.Add(grdData);
        }

        void grdData_LayoutUpdated(object sender, EventArgs e)
        {
            if (bGridInitialized)
                return;
            for (int i = 0; i < grdData.Columns.Count; i++)
            {
                DataGridColumn dgc = grdData.Columns[i];
                LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(dgc.ActualWidth, GridUnitType.Auto) });
            }
            for (int i = 0; i < Headers.Count; i++)
            {
                int ColPos = 0;
                for (int j = 0; j < Headers[i].RowHeaderCells.Count; j++)
                {
                    DynamicHeaderCell dhc = Headers[i].RowHeaderCells[j];
                    AddHeaderTextBlock(LayoutRoot, dhc.Header, i, ColPos, dhc.RowSpan, dhc.ColSpan);
                    ColPos += dhc.ColSpan;
                }
            }

            String[] Footer = SalseData.GetSalseDataFooter().Split('|');
            for(int i =0; i<Footer.Length; i++)
                AddHeaderTextBlock(LayoutRoot, Footer[i], Headers.Count + 1, i, 1, 1);

            Grid.SetRow(grdData, Headers.Count);
            Grid.SetColumnSpan(grdData, grdData.Columns.Count);
            grdData.HeadersVisibility = DataGridHeadersVisibility.None;
            bGridInitialized = true;
        }

        void AddHeaderTextBlock(Grid container, String header, int Row, int Col, int RowSpan, int ColSpan)
        {
            if (RowSpan > Headers.Count - Row)
                RowSpan = Headers.Count - Row;
            if (RowSpan <= 0)
                RowSpan = 1;
            Border b = new Border();
            if (Col + ColSpan == grdData.Columns.Count && Row != Headers.Count + 1)
                b.BorderThickness = new Thickness(1, 1, 1, 0);
            else if (Row != Headers.Count + 1)
                b.BorderThickness = new Thickness(1, 1, 0, 0);
            else if (Col + ColSpan == grdData.Columns.Count && Row == Headers.Count + 1)
                b.BorderThickness = new Thickness(1, 0, 1, 1);
            else
                b.BorderThickness = new Thickness(1, 0, 0, 1);

            b.BorderBrush = grdData.BorderBrush;

            TextBlock tb = new TextBlock();
            tb.Text = header;
            tb.HorizontalAlignment = HorizontalAlignment.Center;
            tb.VerticalAlignment = VerticalAlignment.Center;


            LayoutRoot.Children.Add(b);
            Grid.SetRow(b, Row);
            Grid.SetColumn(b, Col);
            Grid.SetRowSpan(b, RowSpan);
            Grid.SetColumnSpan(b, ColSpan);

            LayoutRoot.Children.Add(tb);
            Grid.SetRow(tb, Row);
            Grid.SetColumn(tb, Col);
            Grid.SetRowSpan(tb, RowSpan);
            Grid.SetColumnSpan(tb, ColSpan);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Somnath Pal
Architect Fountainhead Software Solutions Private Limited
India India
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 15 Dec 2008
Article Copyright 2008 by Somnath Pal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid