asp.net gridview设置未绑定字段的格式

时间:2010-05-14 15:11:32

标签: c# asp.net gridview

我有一个由以下代码填充的gridview:

protected void CautaProiect_Click(object sender, EventArgs e)
        {
            wipDBTableAdapters.GetSummaryProiectTableAdapter proiecte = new wipDBTableAdapters.GetSummaryProiectTableAdapter();
            SummaryGrid.DataSource = proiecte.GetData(CodProiect.Text);
            SummaryGrid.DataBind();
        }

gridview将填充一些带有值的列。 问题是这些值的格式如1234.5600,我希望它们像1,234.56

我是怎么做到的?

4 个答案:

答案 0 :(得分:1)

您可以在OnRowDatabound事件中格式化数据

样品:

    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);
        }
    }

答案 1 :(得分:0)

在GridView列中,使用DataFormatString属性并将其设置为您喜欢的格式。在您的情况下,您将要使用“N2”。

可以找到其他格式选项的精选备忘单here

答案 2 :(得分:0)

您可以使用以下代码在gridview ItemTemplate

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

答案 3 :(得分:0)

我终于找到了答案:

以下是:

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("#,#.##");              
         }