Click here to Skip to main content
16,014,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In default.aspx page, my gridview code is like,
ASP.NET
<asp:GridView ID="gvCategory" runat="server" BorderWidth="1px" BackColor="#C6DEFF"
            Width="100%" BorderStyle="None" BorderColor="#DEBA84" AlternatingRowStyle-BackColor="#A0CFEC"
            AutoGenerateColumns="false">
            <FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center"></PagerStyle>
            <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="	#15317E"></HeaderStyle>
            <Columns>
                <asp:BoundField DataField="Amount" HeaderText="Amount" />
            </Columns>
        </asp:GridView>

In default.aspx.cs page,
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable aTable = new DataTable();
            aTable.Columns.Add("Amount", typeof(string));
           
            DataRow dr;
            dr = aTable.NewRow();
            dr[0] = "560.75";
            aTable.Rows.Add(dr);

            dr = aTable.NewRow();
            dr[0] = "560.5";
            aTable.Rows.Add(dr);

            dr = aTable.NewRow();
            dr[0] = "560";
            aTable.Rows.Add(dr);

            dr = aTable.NewRow();
            dr[0] = "560.05";
            aTable.Rows.Add(dr);

            gvCategory.DataSource = aTable;
            gvCategory.DataBind();
        }
    }

I got the output as -
560.75
560.5
560
560.05

But I want the output as,
560.75
560.50
560.00
560.05

Please give me a solution.
Posted
Updated 26-Dec-11 6:21am
v6

Add the DataFormatString in Boundfiled..

For more information, go through the below link
msdn[^]
 
Share this answer
 
Comments
N!dh!sh 26-Dec-11 9:40am    
I tried this.
<asp:BoundField DataField="Amount" HeaderText="Amount" DataFormatString="{0:C2}" />
But nothing happened.
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip(v=vs.80).aspx
 
Share this answer
 
In default.aspx page, I changed the code like,
ASP.NET
<asp:GridView ID="gvCategory" runat="server" BorderWidth="1px" BackColor="#C6DEFF"
            Width="100%" BorderStyle="None" BorderColor="#DEBA84" AlternatingRowStyle-BackColor="#A0CFEC"
            AutoGenerateColumns="false" OnRowDataBound="GV_OnRowDataBound">
            <FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center"></PagerStyle>
            <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="	#15317E"></HeaderStyle>
            <Columns>
                <asp:TemplateField HeaderText="Amount">
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" Text='<%# Eval("Amount", "{0:F2}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


In default.aspx.cs page, included OnRowDataBound event,
C#
protected void GV_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            ((Label)e.Row.FindControl("lbl")).Text = Convert.ToDecimal(((DataRowView)e.Row.DataItem)["Amount"]).ToString("F2");
    }

This http://shawpnendu.blogspot.com/2009/04/how-to-format-gridview-rowscolumns-in.html[^] link helped me to solve this problem.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900