Decimal Places in Grid

2 posts, 1 answers
  1. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 09 Jun 2011 Link to this post

    I have a grid where I am showing a price field with 5 decimal places, but I only want to show decimal places when I need them.

    e.g.
    1,234.56000 >>> I would like to look like  >>>> 1,234.56

    Although in the same grid I could have >>>>> 0.98765

    Thanks,
    Chuck
  2. Answer
    Martin
    Admin
    Martin avatar
    811 posts

    Posted 15 Jun 2011 Link to this post

    Hello Chuck,

    In RadGrid for ASP .NET AJAX you can easily achieve the desired output by supplying the correct format string in the column in question. Bellow is the code for a sample page that demonstrates this approach:

    <%@ Page Language="C#" %>
     
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <head id="Head1" runat="server">
    </head>
    <body>
        <form id="mainForm" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager1">
        </asp:ScriptManager>
        <script type="text/C#" runat="server">
            protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                System.Data.DataTable table = new System.Data.DataTable();
                table.Columns.Add("ID", typeof(int));
                table.Columns.Add("Number", typeof(decimal));
                table.Rows.Add(1, 1.25300);
                table.Rows.Add(2, 1.2555);
                table.Rows.Add(3, 1);
                table.Rows.Add(4, 1.2500);
                table.Rows.Add(5, 1.2000);
                table.Rows.Add(6, 1.25123);
                RadGrid1.DataSource = table;
            }
         
        </script>
        <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
            AutoGenerateColumns="false">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Number" HeaderText="Number" DataType="System.Decimal"
                        DataFormatString="{0:#.#####}">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <br />
        </form>
    </body>
    </html>

    I hope this helps.

    Regards,
    Martin
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top