Link to home
Start Free TrialLog in
Avatar of madkrell
madkrell

asked on

Formatting dynamic Gridview for decimal data type

Hi Experts,
I am attempting to format a Gridview at runtime that returns Decimal values but shows 4 decimal places whereas I want  to either show 2 decimal places or use {c} or "{0:c}" to get a currency format.
What am I doing wrong please?
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string text = e.Row.Cells[3].Text;
            e.Row.Cells[3].Text = Server.HtmlDecode(text);
            text = string.Format("{0:c}", text);
        }

Open in new window

Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Try this:
"{0:n2}"
Hi,

You have to first check the datatype

then as per datatype you have to change the CellStyle properties.
Avatar of madkrell
madkrell

ASKER

samirbhogayta...  Can you explain what you mean by providing an example?  I already know the datatype that's returned?
s_chilkury... Thanks but your suggestion still has no affect in the Gridview.
I've also tried this but still no change?
if (e.Row.RowType == DataControlRowType.DataRow )
                    {
                    try
                    {
                    string name = e.Row.Cells[0].Text.ToString();
                    e.Row.Cells[1].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[2].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[3].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[4].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[5].Attributes.Add("Style", "{0:c}");
                    ...
                    }
                    catch { }
                    } 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of madkrell
madkrell

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.