Click here to Skip to main content
12,302,764 members (71,277 online)
Click here to Skip to main content

Stats

66.2K views
1.1K downloads
23 bookmarked
Posted

Customizing Group Row Header of Silverlight DataGrid

, 20 Jan 2011 CPOL
This article will definitely help you. Here, I will show you how to modify the XAML to add different content to create a multi level row group header too.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using DataGridDemo1.Providers;
using DataGridDemo1.Models;

namespace DataGridDemo1.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private string searchKey;
        private PagedCollectionView employees;

        /// <summary>
        /// Gets or sets the employees.
        /// </summary>
        /// <value>The employees.</value>
        public PagedCollectionView Employees
        {
            get { return employees; }
            set
            {
                employees = value;
                OnPropertyChanged("Employees");
            }
        }

        public MainViewModel()
        {
            // Fetch the Employee Details from provider and set to Employees collection
            Employees = new PagedCollectionView(EmployeeProviders.GetEmployeeDetails());
        }

        /// <summary>
        /// Groups the name of the data by column.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        public void GroupDataByColumnName(string groupName)
        {
            //Employees.GroupDescriptions.Clear();
            Employees.GroupDescriptions.Add(new PropertyGroupDescription(groupName));
        }

        /// <summary>
        /// Filters the data by search key.
        /// </summary>
        /// <param name="searchKey">The search key.</param>
        public void FilterDataBySearchKey(string searchKey)
        {
            this.searchKey = searchKey;
            Employees.Filter = null;
            Employees.Filter=new Predicate<object>(FilterRecords);
        }

        /// <summary>
        /// Filters the records.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        private bool FilterRecords(object obj)
        {
            var employee = obj as Employee;

            return employee != null &&
                   (employee.Firstname.ToLower().IndexOf(searchKey) >= 0 ||
                    employee.Lastname.ToLower().IndexOf(searchKey) >= 0 ||
                    employee.City.ToLower().IndexOf(searchKey) >= 0 ||
                    employee.State.ToLower().IndexOf(searchKey) >= 0);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

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

_Kunal Chowdhury_
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 20 Jan 2011
Article Copyright 2011 by _Kunal Chowdhury_
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid