Customize an appearance of the PivotGrid's value

3 posts, 1 answers
  1. Lawrence
    Lawrence avatar
    11 posts
    Member since:
    Jun 2015

    Posted 26 Oct 2017 Link to this post

    Hi,

    I have a PivotGrid table with huge value. How do I customize the appearance of the value in the table using my sample javascript below?

    function FormatNumber(value) {
        if (value === 0) { return 0 }
        if (value <= 999) { return value }
        if (value >= 1000 && value <= 999999) { return parseFloat(value / 1000).toFixed(1) + 'K' }
        if (value >= 1000000 && value <= 999999999) { return parseFloat(value / 1000000).toFixed(1) + 'M' }
        if (value >= 1000000000 && value <= 999999999999) { return parseFloat(value / 1000000000).toFixed(1) + 'B' }
        if (value >= 1000000000000 && value <= 999999999999999) { return parseFloat(value / 1000000000000).toFixed(1) + 'T' }
        if (value >= 1000000000000000 && value <= 999999999999999999) { return parseFloat(value / 1000000000000000).toFixed(1) + 'Q }
        return value
    }

     

    Thanks.

  2. Lawrence
    Lawrence avatar
    11 posts
    Member since:
    Jun 2015

    Posted 30 Oct 2017 in reply to Lawrence Link to this post

    Finally, I got the solutions using C# code behind.

    protected void RadPivotGrid1_CellDataBound(object sender, Telerik.Web.UI.PivotGridCellDataBoundEventArgs e)
    {
        if (e.Cell is PivotGridDataCell)
        {
            PivotGridDataCell cell = e.Cell as PivotGridDataCell;
     
            if (cell != null && cell.CellType == PivotGridDataCellType.DataCell)
            {
                if (cell.ParentRowIndexes[1].ToString() == "Sum of Price")
                {
                    //cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF2B1C");
                    switch ((cell.Field as PivotGridAggregateField).DataField)
                    {
                        case "Price":
                            if (cell.DataItem != null && cell.DataItem.ToString().Length > 0)
                            {
                                double price = Convert.ToInt32(cell.DataItem);
                                if (price >= 1000 && price <= 999999)
                                { decimal x = (decimal)price / 1000; cell.Text = string.Format("{0:#,0.00}", x) + " K"; }
                                else if (price >= 1000000 && price <= 999999999)
                                { decimal x = (decimal)price / 1000000; cell.Text = string.Format("{0:#,0.00}", x) + " M"; }
                                else if (price >= 1000000000 && price <= 999999999999)
                                { decimal x = (decimal)price / 1000000000; cell.Text = string.Format("{0:#,0.00}", x) + " B"; }
                                else if (price >= 1000000000000 && price <= 999999999999999)
                                { decimal x = (decimal)price / 1000000000000; cell.Text = string.Format("{0:#,0.00}", x) + " T"; }
                                else if (price >= 1000000000000000 && price <= 999999999999999999)
                                { decimal x = (decimal)price / 1000000000000000; cell.Text = string.Format("{0:#,0.00}", x) + " Q"; }
                                else { }
                            }
                            break;
                    }//end switch
                }
            }
            if (cell != null && cell.CellType == PivotGridDataCellType.ColumnGrandTotalDataCell)
            {
                if (cell.ParentColumnIndexes[0].ToString() == "Grand Total")
                {
                    //cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#1cff7b");
                    switch ((cell.Field as PivotGridAggregateField).DataField)
                    {
                        case "Price":
                            if (cell.DataItem != null && cell.DataItem.ToString().Length > 0)
                            {
                                double price = Convert.ToInt32(cell.DataItem);
                                if (price >= 1000 && price <= 999999)
                                { decimal x = (decimal)price / 1000; cell.Text = string.Format("{0:#,0.00}", x) + " K"; }
                                else if (price >= 1000000 && price <= 999999999)
                                { decimal x = (decimal)price / 1000000; cell.Text = string.Format("{0:#,0.00}", x) + " M"; }
                                else if (price >= 1000000000 && price <= 999999999999)
                                { decimal x = (decimal)price / 1000000000; cell.Text = string.Format("{0:#,0.00}", x) + " B"; }
                                else if (price >= 1000000000000 && price <= 999999999999999)
                                { decimal x = (decimal)price / 1000000000000; cell.Text = string.Format("{0:#,0.00}", x) + " T"; }
                                else if (price >= 1000000000000000 && price <= 999999999999999999)
                                { decimal x = (decimal)price / 1000000000000000; cell.Text = string.Format("{0:#,0.00}", x) + " Q"; }
                                else { }
                            }
                            break;
                    }//end switch
                }
            }
        }           
    }
  3. Answer
    Eyup
    Admin
    Eyup avatar
    3694 posts

    Posted 31 Oct 2017 Link to this post

    Hi Lawrence,

    Yes, the CellDataBound event handler is the right place to achieve this requriement. You can also check the following resource for additional info:
    http://www.telerik.com/blogs/aspnet-pivot-table-made-easy

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top