Show / Hide Table of Contents

KryptonOutlookGrid - Custom Grouping

Create a new OutlookGridGroup

You may want to group your data in another way that the one provided by the grid. Please take a look to the following steps :

    1. Create a new class
    1. Overrides the Text property. Used for the text associated to the group and displayed in the grid.
    1. Overrides the Value. Used for the value of the group that allows to select rows in the group and to sort groups between them.
    1. Overrides the Clone() method.
    1. Overrides the CompareTo() method.
using System;
using Krypton.Toolkit;
using System.Globalization;
using System.Windows.Forms;

namespace KryptonOutlookGrid.SandBox
{
    public class OutlookGridPriceGroup : OutlookGridDefaultGroup
    {

        private int _priceCode;
        private string _currency;

        private const int NO_PRICE = 999999;
        public OutlookGridPriceGroup() : base()
        {
            AllowHiddenWhenGrouped = false;
            _currency = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// The parentGroup if any.</param>
        public OutlookGridPriceGroup(IOutlookGridGroup parentGroup) : base(parentGroup)
        {
            AllowHiddenWhenGrouped = false;
            _currency = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol;
        }

        /// <summary>
        /// Gets or sets the displayed text.
        /// </summary>
        public override string Text => $"{Column.DataGridViewColumn.HeaderText}: {GetPriceString(_priceCode)} ({ItemCount == 1 ? OneItemText : ItemCount.ToString() + XXXItemsText)})";

        private int GetPriceCode(decimal price)
        {
            if ((price == 0))
            {
                return 0;
            }
            else if ((price > 0) && (price <= 100))
            {
                return 100;
            }
            else if ((price > 100) && (price <= 200))
            {
                return 200;
            }
            else if ((price > 200) && (price <= 300))
            {
                return 300;
            }
            else if ((price > 300) && (price <= 600))
            {
                return 600;
            }
            else if ((price > 600) && (price <= 1000))
            {
                return 1000;
            }
            else if ((price > 1000) && (price <= 5000))
            {
                return 5000;
            }
            else if ((price > 5000) && (price <= 10000))
            {
                return 10000;
            }
            else if (price > 10000)
            {
                return 20000;
            }
            else
            {
                return 999999;
            }
        }


        private string GetPriceString(int priceCode)
        {
            switch (priceCode)
            {
                case 0:
                    return "Free";
                case 100:
                    return "Below 100 " +  _currency;
                case 200:
                    return "Between 100 and 200 " + _currency;
                case 300:
                    return "Between 200 and 300 " + _currency;
                case 600:
                    return "Between 300 and 600 " + _currency;
                case 1000:
                    return "Between 600 and 1000 " + _currency;
                case 5000:
                    return "Between 1000 and 5000 " + _currency;
                case 10000:
                    return "Between 5000 and 10000 " + _currency;
                case 20000:
                    return "Above 10000 " + _currency;
                case 999999:
                    return "No price";
                default:
                    return "";
            }
        }


        /// <summary>
        /// Gets or sets the Alphabetic value
        /// </summary>
        public override object Value
        {
            get { return val; }
            set
            {
                if (object.ReferenceEquals(value, DBNull.Value) || value == null)
                {
                    _priceCode = NO_PRICE;
                    val = _priceCode;
                }
                else
                {
                    _priceCode = GetPriceCode(decimal.Parse(value.ToString()));
                    val = _priceCode;
                }
            }
        }

        #region "ICloneable Members"

        /// <summary>
        /// Overrides the Clone() function
        /// </summary>
        /// <returns>OutlookGridAlphabeticGroup</returns>
        public override object Clone()
        {
            OutlookGridPriceGroup gr = new OutlookGridPriceGroup(this.ParentGroup);

            gr.Column = this.Column;
            gr.Value = this.val;
            gr.Collapsed = this.Collapsed;
            gr.Height = this.Height;
            gr.GroupImage = this.GroupImage;
            gr.FormatStyle = this.FormatStyle;
            gr.XXXItemsText = this.XXXItemsText;
            gr.OneItemText = this.OneItemText;
            gr.AllowHiddenWhenGrouped = this.AllowHiddenWhenGrouped;
            gr.SortBySummaryCount = this.SortBySummaryCount;
            gr._currency = _currency;
            gr._priceCode = _priceCode;
            return gr;
        }

        #endregion

        #region "IComparable Members"
        /// <summary>
        /// Override the CompareTo, so only the first character is compared, instead of the whole string
        /// this will result in classifying each item into a letter of the Alphabet.
        /// for instance, this is useful when grouping names, they will be categorized under the letters A, B, C etc..
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override int CompareTo(object obj)
        {
            int orderModifier = (Column.SortDirection == SortOrder.Ascending ? 1 : -1);
            int priceOther = 0;

            if (obj is OutlookGridPriceGroup)
            {
                priceOther = ((OutlookGridPriceGroup)obj)._priceCode;
            }
            else
            {
                priceOther = NO_PRICE;
            }
            return _priceCode.CompareTo(priceOther) * orderModifier;
        }
        #endregion
    }
}
Back to top Krypton Component Suite 2024 BSD 3-Clause License © Component Factory Pty Ltd, 2006 - 2016, All rights reserved. Modifications by Peter Wagner (aka Wagnerp), Simon Coghlan (aka Smurf-IV), Giduac & Ahmed Abdelhameed et al. 2017 - 2025. All rights reserved. https://github.com/Krypton-Suite/Standard-Toolkit