views:

866

answers:

3

Hi,

I am using an ASP.NET GridView control with a TemplateColumn which has a TextBox in the ItemTemplate. I am binding the Text of the TextBox with the values from database. The database holds these values with five decimal points, and for the interface I would like to display only two decimals.

The user can make changes to this grid. When I'm saving, I would like to be able to get all five decimals for the values that are not changed. Since it is displayed as two decimals, I was only able to read two decimals on postback? Is there a way around this other than saving the actual value in a hidden variable?

Thanks,

sridhar

A: 

You could always validate that the data has indeed changed at all in that column before updating it.

You could potentially add some sort of stored SQL procedure that would take the place of the column name that would do the compare before hand? Seems like a stretch.

Otherwise, yeah, a hidden field with some extra processing being done to hide/modify the original data of the field seems like the only answer. Anyway you cut it, there's going to be some quirky logic somewhere (whether it's in the database, or code that runs when you perform the post, or in some hidden field).

altCognito
A: 

Compare the textbox text with that of rounded (2 decimal places) value of the DataKey bound that textbox on that gridrow.

Note: Make sure you get the rounding right!!!

If they are equal, that means the user hasn't changed the value and you save the datakey value as is. (Even if he has changed the value, it is irrelevant in logical terms)

If they aren't, that means the user has changed the value of the textbox and you need to use his value while saving.

orezavi
+1  A: 

I have a formatCurrency plugin that could help you out here. What you'll want to do is display the data in a textbox and use a hiddenfield to bind the data. This way, not only do you retain the decimal places, but you can accept more than two decimal places as input.

Here is a sample that should work.

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;
</script>
<script type="text/javascript" src="jquery.formatCurrency.js">
</script>
<script type="text/javascript">
    $(document).ready(function() {
        $('.editNumber').formatCurrency();
        $('.editNumber').blur(function(e) {
            var val = $(this).toNumber();
            $(this).next('input:hidden').val(val)
                .end().formatCurrency();

        });
    });
</script>
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox runat="server" CssClass="editNumber" ID="EditNumber" Text='<%# Eval("Price") %>' />
                <asp:HiddenField runat="server" ID="HiddenNumber" Value='<%# Bind("Price") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
bendewey