开发者

asp.net gridview set format for unbound field

i have a gridview populated by the code below:

protected void CautaProiect_Click(object sender, EventArgs e)
        {
            wipDBTableAdapters.GetSummaryProiectTableAdapter proiecte = new wipDBTableAdapters.GetSummaryProiectTableAdapter(开发者_运维百科);
            SummaryGrid.DataSource = proiecte.GetData(CodProiect.Text);
            SummaryGrid.DataBind();
        }

The gridview will be populated with some columns with values. The problem is that the values are formated like this 1234.5600 and i want them to be like 1,234.56

How ca i do this ?


You can format your data in the OnRowDatabound event

sample:

    protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label l = (Label)e.Row.FindControl("lblValue");
            l.Text = String.Format("{0:C}", l.Text);
        }
    }


In your GridView columns, use the DataFormatString property and set it to the format you prefer. In your case, you'll want to use "N2".

A great cheatsheet of other formatting options can be found here.


you can use the following code to display in the gridview ItemTemplate

  <asp:Label ID="lblFinalPrice" runat="server" Text='<%#Convert.ToDouble(Eval("FinalPrice")).ToString("#.00")%>'></asp:Label>


I have finally managed to find an answer for this :

Here is how :

protected void SummaryGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.DataRow)  
         {  
             double value = Convert.ToDouble(e.Row.Cells[4].Text);  
             e.Row.Cells[4].Text = value.ToString("#,#.##");              
         }  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜