2013-04-09 34 views

回答

2

一種方法是使用DataFormatString屬性。例如:

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="ProductID" DataSourceID="SqlDataSource1"> 
    <Columns> 
     <asp:BoundField DataField="ListPrice" 
      HeaderText="ListPrice" 
      SortExpression="ListPrice" 
      DataFormatString="{0:F1}" /> 
    </Columns> 
</asp:GridView> 

你也可以使用RowDataBound,你有更多的控制權:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // assuming you are using BoundFields and 
     // the column which you want to format is the first 
     // if you are using TemplateFields use e.Row.FindControl("ControlID") to find your controls 
     var row = (DataRowView) e.Row.DataItem; 
     decimal price = (decimal)row["ListPrice"]; 
     e.Row.Cells[0].Text = price.ToString("F1"); 
    } 
} 

編輯:這裏的VB.NET版本:

Protected Sub GridView1_RowDataBound(sender As [Object], e As GridViewRowEventArgs) Handles GridView1.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     ' assuming you are using BoundFields and             ' 
     ' the column which you want to format is the first           ' 
     ' if you are using TemplateFields use e.Row.FindControl("ControlID") to find your controls ' 
     Dim row = DirectCast(e.Row.DataItem, DataRowView) 
     Dim price As Decimal = CDec(row("ListPrice")) 
     e.Row.Cells(0).Text = price.ToString("F1") 
    End If 
End Sub 
1

你只需要使用BoundField.DataFormatString屬性,爲了解決您的問題並獲得適當的知識,請檢查此Microsoft Link

希望它適合你。

相關問題