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, 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 ) );
}
}
all to no avail. The client insists, and reasonably so, that when editing the cell, the value should be displayed with two decimal places.