Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

Silverlight DataGrid with Dynamic Multiple Row Columns in Header

Rate me:
Please Sign up or sign in to vote.
3.86/5 (6 votes)
15 Dec 2008CPOL3 min read 93.2K   3.7K   29  
Creating a dynamic header for the Silverlight DataGrid with multiple rows/columns.
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)


Written By
Architect Fountainhead Software Solutions Private Limited
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions