locked
Decimal formatting within Gridview control RRS feed

  • Question

  • User-1114390349 posted

    I have a gridview control with a item template column as

            <asp:TemplateField HeaderText="TOTAL FEE">
                <ItemTemplate>
                    <asp:Label ID="LTFEE" runat="server" Text=""></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

    on code behind I want to format the column with indian currency with 2 decimal places. I tried this code in RowDataBound event, but it does not format the column.

    (e.Row.FindControl("LTFEE") as Label).Text = string.Format("{0:n2}", DataBinder.Eval(e.Row.DataItem, "TFEE").ToString());

    What I did wrong, please help me in this regard Thank u

    Thursday, December 26, 2019 4:11 PM

All replies

  • User475983607 posted

    Use standard binding and currency format.

    <asp:Label ID="LTFEE" runat="server" Text='<%# Bind("TFEE","{0:C}") %>'></asp:Label>

    The GridView reference docs have this sort of information.  You can also use the standard GridView wizard to accomplish the same.

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview?view=netframework-4.8

    Thursday, December 26, 2019 4:40 PM
  • User-1114390349 posted

    thank u for your reply, I already tried this method but it is not formatting.

    Regards

    Thursday, December 26, 2019 4:56 PM
  • User475983607 posted

    thank u for your reply, I already tried this method but it is not formatting.

    Regards

    I'm unable to reproduce this issue.  My best guess is LTFEE is already a string.  LTFEE must be a numeric type.  You might have to rethink the design,

    Thursday, December 26, 2019 5:05 PM
  • User288213138 posted

    Hi asolmdu,

    on code behind I want to format the column with indian currency with 2 decimal places. I tried this code in RowDataBound event, but it does not format the column

    You can try to use the CultureInfo class, this class provides formatting for dates and numbers about a specific culture.

    The code:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="TOTAL FEE">
                            <ItemTemplate>
                                <asp:Label ID="LTFEE" runat="server" Text='<%# Eval("TFEE") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
    
     protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[1] { new DataColumn("TFEE") });
                dt.Rows.Add(1234567);
                dt.Rows.Add(2345678);
                dt.Rows.Add(9874563);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label l = (Label)e.Row.FindControl("LTFEE");
                    decimal parsed = decimal.Parse(l.Text, CultureInfo.InvariantCulture);
                    CultureInfo hindi = new CultureInfo("hi-IN");
                    l.Text = string.Format("{0:c}", parsed);
                }
            }

    The result:

    Best regards,

    Sam

    Friday, December 27, 2019 3:05 AM
  • User1052662409 posted

    <asp:Label ID="LTFEE" runat="server" Text=""></asp:Label>

    Try this

    <asp:Label ID="LTFEE" runat="server" Text='<%#Eval("TFEE","{0:n}")%>'></asp:Label>

    May this help

    Wednesday, January 22, 2020 6:20 AM