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

Decimal Places in Grid

1 Answer 751 Views
Grid
This is a migrated thread and some comments may be shown as answers.
KawaUser
Top achievements
Rank 2
KawaUser asked on 09 Jun 2011, 10:02 PM
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

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 15 Jun 2011, 08:11 AM
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.

Tags
Grid
Asked by
KawaUser
Top achievements
Rank 2
Answers by
Martin
Telerik team
Share this question
or