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

GridViewDecimalColumn String formatting problem for zeros (0)

1 Answer 98 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Soroush
Top achievements
Rank 1
Soroush asked on 25 Aug 2019, 06:20 PM

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?

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 27 Aug 2019, 04:44 AM
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.
Tags
GridView
Asked by
Soroush
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or