user1725978
user1725978

Reputation: 21

aspx sql gridview "display price*sublength in price column"

when displaying price on a magazine for a subscription i see on my page:

Price | Subscription Length

69.95 | 12

69.95 | 24

the 24 month subscription should show double the 12 month (139.9) but i havent implemented this in the database and it is already built and being used successfully in all other areas.

i came up with a simple formula to fix it but i have no idea where to put it!

i display the table in a gridview in boundviews with a sqldatasource to define what to show

where do i put this and how should it be written?

/* sub equals column sublength divided by 6 and dived by 2, so 12=1 or 24=2 */

sub=sublength/6/2

/* price=column price times sub, sub=1 no change, sub=2 then double */

price=price*sub

Upvotes: 1

Views: 230

Answers (2)

codingbiz
codingbiz

Reputation: 26396

You can do it this way

<asp:GridView runat="server">
<Columns>
   <asp:BoundField DataField="Price" HeaderText="Price" />
   <asp:BoundField DataField="Months" HeaderText="Subscription Length" />

   <asp:TemplateField>
   <ItemTemplate>
      <%# (Convert.ToDecimal(Eval("Price")) * Convert.ToInt32(Eval("Months")) / 12) %>
   </ItemTemplate>
   </asp:TemplateField>
</Columns>
</asp:GridView>

Or this way

protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
   decimal Price = Convert.ToDecimal(e.Row.Cells[0].Text);
   int months = Convert.Int32(e.Row.Cells[1].Text);

   e.Row.Cells[0].Text = Price * (months / 12);
}

Upvotes: 1

Sagar Upadhyay
Sagar Upadhyay

Reputation: 819

For this type of issue you have to use onRowDatabound event of GridView. Put your logic code in that event and all will be happen as you wish, just make sure that you are doing right stuff....

Upvotes: 0

Related Questions