鉴于以下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 :(得分:3)
默认情况下,仅当包含BoundField
对象的数据绑定控件处于只读模式时,格式字符串才会应用于字段值。要在编辑模式下将格式字符串应用于字段值,请将ApplyFormatInEditMode
属性设置为true。
希望有所帮助