This is a migrated thread and some comments may be shown as answers.

Customize an appearance of the PivotGrid's value

2 Answers 127 Views
PivotGrid
This is a migrated thread and some comments may be shown as answers.
Lawrence
Top achievements
Rank 1
Lawrence asked on 26 Oct 2017, 09:12 AM

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 Answers, 1 is accepted

Sort by
0
Lawrence
Top achievements
Rank 1
answered on 31 Oct 2017, 01:34 AM

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
            }
        }
    }           
}
0
Accepted
Eyup
Telerik team
answered on 31 Oct 2017, 07:21 AM
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.
Tags
PivotGrid
Asked by
Lawrence
Top achievements
Rank 1
Answers by
Lawrence
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or