Currency Format String

6 posts, 0 answers
  1. Rosendo Hernandez Gomez
    Rosendo Hernandez Gomez avatar
    4 posts
    Member since:
    Aug 2007

    Posted 02 Aug 2008 Link to this post

    Hi I am using RadControlsWinforms_Q2_2008, and I have a Problem

    I am using a RadGrid and I add the columns using the Property Builder. Then I add a textboxcolumn and put a {0:c} on the    FormatString, but whe the Grid is displayed it ignores the format.

    Thank you
  2. Jordan
    Admin
    Jordan avatar
    547 posts

    Posted 04 Aug 2008 Link to this post

    Hi Rosendo Hernandez Gomez,

    This can happen if the underlying data field is of non-numeric type (for example String).

    If that is the case and you still want to apply currency formatting you can handle the CellFormatting event like:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
            { 
                if (e.CellElement.ColumnInfo.HeaderText == "PostalCode"
                { 
                    double value = 0.0; 
                    if (double.TryParse(e.CellElement.RowInfo.Cells["PostalCode"].Value.ToString(), out value)) 
                    { 
                        e.CellElement.Text = string.Format(e.CellElement.FormatString, value); 
                    } 
                } 
            } 

    Else if that is not the case we will need some more information like for example:
    • Are you using RadGridView in bound or unbound mode?
    • If the grid is in bound mode what is the type of the data for that column in the datasource and what kind is the datasource?
    I am looking forward to your reply.
    Best wishes,
    Jordan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Rosendo Hernandez Gomez
    Rosendo Hernandez Gomez avatar
    4 posts
    Member since:
    Aug 2007

    Posted 04 Aug 2008 Link to this post

    Hi Jordan, This is not the case, he underlying data is a double, I am binding the GridView to an ArrayList that has instances of a class.

    CargoOtrosServicios Cargo = new CargoOtrosServicios();
    Cargo.CveConcepto = "1";
    Cargo.Concepto = "Inscripcion";
    Cargo.Costo = 150.55;
    OtrosServicios.Add(Cargo);

    BindingSource bindingOtrosServicios = new BindingSource();
    bindingOtrosServicios.DataSource = OtrosServicios;
    radGridView.DataSource = bindingOtrosServicios;

    An this is my Class


            public class CargoOtrosServicios
            {
                private string cveconcepto;
                private string concepto;
                private double costo;

                public string CveConcepto
                {
                    get
                    {
                        return cveconcepto;
                    }
                    set
                    {
                        cveconcepto = value;
                    }
                }
                public string Concepto
                {
                    get
                    {
                        return concepto;
                    }
                    set
                    {
                        concepto = value;
                    }
                }

                public double Costo
                {
                    get
                    {
                        return costo;
                    }
                    set
                    {
                        costo = value;
                    }
                }
            }

    Thank you
  4. Jordan
    Admin
    Jordan avatar
    547 posts

    Posted 07 Aug 2008 Link to this post

    Hello Rosendo Hernandez Gomez,

    After some testing I discovered the reason for your problem.
    There is a bug that prevents RadGridView from serializing the FormatString in design-time. It will be fixed for the Q2 2008 SP1 release, and for now the solution is to create the columns manually for example like in the code below:
    this.radGridView1.Columns.Clear(); 
     
                this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false
     
                GridViewTextBoxColumn conceptColumn = new GridViewTextBoxColumn("Concepto"); 
                this.radGridView1.Columns.Add(conceptColumn); 
     
                GridViewTextBoxColumn costColumn = new GridViewTextBoxColumn("Costo"); 
                this.radGridView1.Columns.Add(costColumn); 
     
                this.radGridView1.DataSource = bindingOtrosServicios; 
     
                this.radGridView1.Columns["Costo"].FormatString = "{0:c}"

    Don't hesitate to contact us if you have other questions.

    All the best,
    Jordan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Nishant
    Nishant avatar
    1 posts
    Member since:
    Nov 2017

    Posted 06 Dec 2017 in reply to Jordan Link to this post

    Hi Telerik Team,

     

    I am passing (16,2) eg. 1234567891234567.32  price in gridviewdecimalcolumn but when I am converting to double its giving abrupt values

    double.TryParse(e.CellElement.RowInfo.Cells[e.Column.Name].Value.ToString(), out value) -- result -- 1,234,567,891,234,570

     

    -- and e.value.Tostring()-- 1.23456789123457E+15

    Kindly suggest

    Can mail me at nishu61988@gmail.com

  6. Dess
    Admin
    Dess avatar
    2748 posts

    Posted 11 Dec 2017 Link to this post

    Hello, Nishant,

    Thank you for writing.  

    For currency values, it is recommended to use decimal type. It is more precise. For a conversion from decimal to float or double, the decimal value is rounded to the nearest double or float value. While this conversion may lose precision, it never causes an exception to be thrown. The following StackOverflow thread is quite useful on this topic: https://stackoverflow.com/questions/1584314/conversion-of-a-decimal-to-double-number-in-c-sharp-results-in-a-difference

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    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