簡體   English   中英

ASP.NET GridView:如何在編輯模式下控制列的格式?

[英]ASP.NET GridView: How to Control the Format of a Column in Edit Mode?

給定以下GridView

<asp:GridView runat="server" ID="GridMenuItemAttributes" DataKeyNames="MenuItemAttributeID" AutoGenerateColumns="false" 
        OnRowCommand="GridMenuItemAttributes_RowCommand" DataSourceID="DSMenuItemAttributes" OnRowEditing="GridMenuItemAttributes_RowEditing" >
<Columns>
    <asp:BoundField HeaderText="Description" DataField="DisplayName" />
    <asp:BoundField HeaderText="Price" DataField="Price" DataFormatString="{0:F2}" />
    <asp:CommandField ShowEditButton="true" ShowDeleteButton="true"
            EditText="Edit" DeleteText="Delete" />
</Columns>
</asp:GridView>

當查看一行時,“ Price字段的格式正確地用小數點后兩位表示,但是當我編輯一行時,其Price字段則變為小數點后四位。 我嘗試了不同的格式(例如“ C”,“ 0.00”),並附加了以下OnRowEditing處理程序:

protected void GridMenuItemAttributes_RowEditing( object sender, GridViewEditEventArgs e )
{
    int menuItemAttributeID = Convert.ToInt32( GridMenuItemAttributes.DataKeys[ e.NewEditIndex ].Value );

    if ( ! String.IsNullOrEmpty( GridMenuItemAttributes.Rows[ e.NewEditIndex ].Cells[ 1 ].Text ) )
    {
        String theValue = GridMenuItemAttributes.Rows[ e.NewEditIndex ].Cells[ 1 ].Text;
        GridMenuItemAttributes.Rows[ e.NewEditIndex ].Cells[ 1 ].Text = String.Format( "{0:0.00}", Convert.ToDouble( theValue ) );
    }
}

一切都無濟於事。 客戶堅持並合理地要求,在編輯單元格時,該值應顯示兩位小數。

默認情況下,僅當包含BoundField對象的數據綁定控件處於只讀模式時,格式字符串才應用於字段值。 要在編輯模式下將格式設置字符串應用於字段值,請將ApplyFormatInEditMode屬性設置為true。

來自: MSDN DataFormatString

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM