代码之家  ›  专栏  ›  技术社区  ›  Iulian

未绑定字段的ASP.NET网格视图集格式

  •  0
  • Iulian  · 技术社区  · 15 年前

    我用下面的代码填充了一个网格视图:

    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 回复  |  直到 15 年前
        1
  •  1
  •   TheGeekYouNeed    15 年前

    可以在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);
            }
        }
    
        2
  •  0
  •   Dillie-O    15 年前

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

    可以找到其他格式选项的一个很好的作弊表。 here .

        3
  •  0
  •   Basant B. Pandey    15 年前

    可以使用以下代码在GridView项模板中显示

      <asp:Label ID="lblFinalPrice" runat="server" Text='<%#Convert.ToDouble(Eval("FinalPrice")).ToString("#.00")%>'></asp:Label>
    
        4
  •  0
  •   Iulian    15 年前

    我终于找到了答案:

    以下是如何:

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