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

Currency Format String

5 Answers 802 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rosendo Hernandez Gomez
Top achievements
Rank 1
Rosendo Hernandez Gomez asked on 02 Aug 2008, 04:19 PM
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

5 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 04 Aug 2008, 09:47 AM
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.
0
Rosendo Hernandez Gomez
Top achievements
Rank 1
answered on 04 Aug 2008, 01:32 PM
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
0
Jordan
Telerik team
answered on 07 Aug 2008, 07:13 AM
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.
0
Nishant
Top achievements
Rank 1
answered on 07 Dec 2017, 04:01 AM

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

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 11 Dec 2017, 09:01 AM
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.
Tags
GridView
Asked by
Rosendo Hernandez Gomez
Top achievements
Rank 1
Answers by
Jordan
Telerik team
Rosendo Hernandez Gomez
Top achievements
Rank 1
Nishant
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or