Display amount with 2 digit after decimal place in ASP.Net GridView

Sumeet
 
on Apr 19, 2018 07:34 AM
Sample_134275.zip
5872 Views

Sir,

I have a grid to display amount. I am getting result of amount as with 4 zeros after decimal point. But I want to display an amount with only 2 digit after decimal. How to do this ?

Html View:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>       
       <asp:TemplateField>
       <ItemTemplate>
           <asp:Label id="lb1" runat="server" Text='<%# Bind("Id") %> '></asp:Label>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField><ItemTemplate>
           <asp:Label ID="savings" runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "savings", "{0:N2}") %>' />
           </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField><ItemTemplate>
           <asp:Label id="bonus1" runat="server" Text='<%# Eval("bonus1","{0:N2}") %>'></asp:Label>
           </ItemTemplate>
       </asp:TemplateField>
        <%--<asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" ItemStyle-Width="150px" />
        <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />--%>
    </Columns>
</asp:GridView>

c# code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                            new DataColumn("savings", typeof(string)),
                            new DataColumn("bonus1",typeof(string)) });
            dt.Rows.Add(1, "8.0000", "89.0000");
            dt.Rows.Add(2, "658.0000", "56.0000");
            dt.Rows.Add(3, "32652.0000", "5698.0000");
            dt.Rows.Add(4, "215.0000", "458.0000");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 19, 2018 07:39 AM
on Apr 19, 2018 07:40 AM

Hi Sumeet,

In order to display in decimal format you need to change the data type of the column savings and bonus1 to decimal.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>
                <asp:Label ID="lb1" runat="server" Text='<%# Bind("Id") %> '></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Savings">
            <ItemTemplate>
                <asp:Label ID="savings" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "savings", "{0:N2}") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Bonus">
            <ItemTemplate>
                <asp:Label ID="bonus1" runat="server" Text='<%# Eval("bonus1","{0:N2}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("savings", typeof(decimal)),
                        new DataColumn("bonus1",typeof(decimal)) });
        dt.Rows.Add(1, "8.0000", "89.0000");
        dt.Rows.Add(2, "658.0000", "56.0000");
        dt.Rows.Add(3, "32652.0000", "5698.0000");
        dt.Rows.Add(4, "215.0000", "458.0000");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("savings", GetType(Decimal)), New DataColumn("bonus1", GetType(Decimal))})
        dt.Rows.Add(1, "8.0000", "89.0000")
        dt.Rows.Add(2, "658.0000", "56.0000")
        dt.Rows.Add(3, "32652.0000", "5698.0000")
        dt.Rows.Add(4, "215.0000", "458.0000")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Output

IdSavingsBonus
1 8.00 89.00
2 658.00 56.00
3 32,652.00 5,698.00
4 215.00 458.00