Given the following 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>
The Price
field correctly formats with two decimal places when viewing a row, but changes to four decimal places when I'm editing a row. I've tried different formats (e.g., "C", "0.00") and attached the following OnRowEditing handler:
protected void GridMenuItemAttributes_RowEditing( object sender, GridV开发者_开发知识库iewEditEventArgs 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 ) );
}
}
all to no avail. The client insists, and reasonably so, that when editing the cell, the value should be displayed with two decimal places.
By default, the formatting string is applied to the field value only when the data-bound control that contains the BoundField
object is in read-only mode. To apply the formatting string to field values while in edit mode, set the ApplyFormatInEditMode
property to true.
From: MSDN DataFormatString
Hope that helps
精彩评论