2009-12-03 28 views
0

有沒有辦法將gridview列腳註值的格式設置爲貨幣。我將BoundField的DataFormatString設置爲{0:c},但這似乎不影響頁腳。我是否必須在RowDataBound事件中執行此操作?Gridview頁腳數據格式字符串

回答

0

我結束了在類似這樣的方式做...

protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    double dVal = 0.0; 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     dVal += Convert.ToDouble(e.Row.Cells[1].Text); 
    } 

    if (e.Row.RowType == DataControlRowType.Footer) 
    { 
     e.Row.Cells[1].Text = dVal.ToString("c"); 
    } 
} 
0

您是否在頁腳上設置了HtmlEncode =「False」?

編輯:這一直是一個已知的問題。

0

這是一個雜牌組裝電腦。 (sp?)

但是有時候在遇到已知問題時,你得弄爛。 (?SP)

private static string FormatByGridView(object value, string DataFormatString) 
{ 
    GridView grid = new GridView(); 
    DataTable table = new DataTable(); 
    table.Columns.Add("value", value.GetType()); 
    var row = table.NewRow(); 
    row["value"] = value; 
    table.Rows.Add(row); 
    grid.Columns.Add(new BoundField() { DataField = "value", DataFormatString = DataFormatString }); 
    grid.DataSource = table; 
    grid.DataBind(); 
    return grid.Rows[0].Cells[0].Text; 
} 
0

這個工作對我來說:

if (e.Row.RowType == DataControlRowType.Footer) 
{ 
    e.Row.Cells[2].Text = string.Format("{0:#,###,###.00}", total2013); 
    e.Row.Cells[3].Text = string.Format("{0:#,###,###.00}", total2014); 
}