2009-11-19 120 views
1

鑑於以下GridViewASP.NET 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場正​​確地保留兩位小數格式查看行時,但改變四個小數位時,我編輯的行。我已經嘗試了不同的格式(例如, 「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)); 
    } 
} 

都沒有用。客戶堅持併合理地認爲,在編輯單元格時,該值應該顯示兩位小數。

+0

你爲什麼不跟模板列嘗試。使用CompareValidator來驗證具有屬性的金額Operator =「DataTypeCheck」Type =「Currency」 – Saar 2009-11-19 16:52:42

回答

3

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

來源:MSDN DataFormatString

希望幫助

+1

神聖的鯖魚,*這就是所需要的!*從未見過那個。謝謝!!! – 2009-11-20 16:01:38

+0

不客氣:) – keyboardP 2009-11-20 16:27:31

+1

並感謝我!我花了將近一個小時,直到我找到了這個! – fuzzbone 2009-12-21 20:43:33

相關問題