Decimal column leading zeros

4 posts, 0 answers
  1. Danilo
    Danilo avatar
    42 posts
    Member since:
    Sep 2012

    Posted 09 Jul 2015 Link to this post

    Hi there

     I'm trying to add rows to a GridViewDecimalColumn with leading zeros. I know there's a property "FormatString" but this is not exactly what I want. I want the numbers always have the same amount of zeros leading. Here you can see my code:

    GridViewDecimalColumn col = new GridViewDecimalColumn();
    col.FormatString = "{0:0000}";

    This will output the following:

    Number 1 will be 0001
    Number 2 will be 0002
    Number 10 will be 0010
    Number 100 will be 0100

    But what I'm trying to achieve is:

    Number 1 should be 0001
    Number 10 should be 00010
    Number 100 should be 000100

    Is this possible? Hope you can help me.



  2. erwin
    erwin avatar
    382 posts
    Member since:
    Dec 2006

    Posted 10 Jul 2015 in reply to Danilo Link to this post

    Hi Danilo, I guess for this rather unusual formatting, your best option is to define an event handler to for the CellFormatting event.



  3. Danilo
    Danilo avatar
    42 posts
    Member since:
    Sep 2012

    Posted 10 Jul 2015 in reply to erwin Link to this post

    Hi Erwin

    The problem is, it's a DecimalColumn so it's deleting all 0 before the first "real" number.

    Any other ideas? :)


  4. Ralitsa
    Ralitsa avatar
    185 posts

    Posted 10 Jul 2015 Link to this post

    Hi Danilo,

    Thank you for contacting us. 

    You can use the escape character to prevent the formatting operation. Here is the code snippet how to set the FormatString of GridViewDecimalColumn:
    decimalColumn2.FormatString = "{0:\\0\\0\\0#}";

    In the attachments you can find an image which demonstrates the result.  

    You can refer to the following article from MSDN for more information about  Custom Numeric Format Strings, section "The Escape Character".
    I hope this will help you. Let me know if you have any other questions.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top