GridViewDecimalColumn String formatting problem for zeros (0)

2 posts, 0 answers
  1. Soroush
    Soroush avatar
    2 posts
    Member since:
    Aug 2019

    Posted 25 Aug 2019 Link to this post

    Hi everyone!

    I have a couple of columns containing numbers which some of them are zeros (0).I use the following code to format grid cells:

    for(int i = 1; i < 6; i++)
    {
        GridViewDecimalColumn myCol = this.Grid.Columns[i] as GridViewDecimalColumn;
        myCol.FormatString = "{0:###,###,###,###}";
    }

    As  you can tell this code turns this value: 1234567 into this: 1,234,567

    But my problem is that when this formatting is applied, since some of my cells contain the value "0", all of them will be blank and empty. It's like nothing exists in that cell but if I remove the formatting, all zeros are again where they should be.

    I believe this problem is probably because of the type of "GridViewDecimalColumn" class.

    What's your suggestion?

  2. Nadya
    Admin
    Nadya avatar
    273 posts

    Posted 26 Aug 2019 Link to this post

    Hello, Soroush,

    If you want the use the specified formatting and show cells with values 0, you can subscribe to the CellFormatting event and set the CellElement.Text property to 0. Please refer to the following code snippet:

    private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.Column is GridViewDecimalColumn && e.CellElement.Value != null)
        {
            if (Convert.ToDouble(e.CellElement.Value.ToString()) == 0)
            {
                e.CellElement.Text = "0";
            }
        }
    }



    I hope this helps. Should you have any other questions, I will be glad to help.

    Regards,
    Nadya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top