dataset/GridView calculation

G

Guest

I have a dataset that is populating my gridview, but I have a data in the
dataset coming out like .011 and showing in the gridView as .011%. I need to
calculate that so it shows as 1.1% and not .011%. How can I do that and still
bind it to my gridView without having to make that calculation on the
database side?
 
G

Guest

that didn't work, I'm still getting:
..011 and not 1.1%

I have this
<ItemTemplateField>
<asp:Label runat=server text='<%#Convert.ToDouble(Eval("value")) * 100
%>'></asp:Label>
</ItemTemplateField>
and its showing in my gridView as .011 still.
 
W

Walter Wang [MSFT]

Hi,

I tried that using Northwind Access database, and it seems the expression
should work. What's the data type of underlyiny field in the DataSet?

Also, actually you don't need to do that convert by hand, GridView has a
DataFormatString that can do that for you:

<asp:BoundField DataField="UnitPrice" DataFormatString="{0:p2}"
HtmlEncode="false" />

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

its a calculation coming from the stored procedure.
Also, I did the expression in a template field, so how will
asp:BoundField DataField="UnitPrice" DataFormatString="{0:p2}"
HtmlEncode="false" /> - this work in a templateField?

I'm using this on other values in the grid but their not templateFields
 
W

Walter Wang [MSFT]

Hi,

Sorry for late reply.

For TemplateField, when using Eval or Bind to get the bound data, it can
use another parameter to specify a format string:

<ItemTemplate>
<asp:Label runat="server" ID="lbl1" Text='<%# Eval("value", "{0:p2}")
%>'></asp:Label>
</ItemTemplate>

However, after I reviewed your question again, I suspect it might still not
work for your case. Since using your "Convert.ToDouble" code on my side
will work correctly, I think it might be related to the data type of the
field "value" in your DataSet.

I've done following test using a VARCHAR field:

1) If I use DataFormatString or Eval/Bind with format string "{0:p2}", it
will not work, it will still display "0.011".

2) If I use your original code 'Convert.ToDouble("value")) * 100', it
should display "1.1" instead.

Is it possible for you to create a small reproducible project to show this
issue? I will have to have such a project to find the root cause of this
issue. Thank you for your effort and understanding.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

John

I got this to work actually. I created a variable in the code behind and
bound that to my grid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top