Solved

asp.net + datagrid + division of two colums

Posted on 2004-04-22
5
402 Views
Last Modified: 2012-05-04
i have a datagrid and have two columns in my datagrid that are numbers i want the third column to display column a / column b and show it as a percentage.
0
Comment
Question by:dkilby
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Higor
ID: 10894737
Ok you have to do you calcul on the ItemDataBound event of the datagrid.

After you get the column 3 and the item in it like e.Item = a/b

Something like that.

Have fun!

0
 

Author Comment

by:dkilby
ID: 10894786
Can you give me an example with code, plus how do i format the column to show as a percent??

thanks
0
 
LVL 5

Accepted Solution

by:
Higor earned 500 total points
ID: 10896601
Check this code:

Public Sub OnItemBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        'Ok simple here just do the division of column a / b
        'like this:


        'Your third column = Your first / your second   You convert the info to double to be sure its correctly divised. After to string to attribute it to a string ;) Of course if option strict is on ;)
        e.Item.Cells(2).Text = Convert.ToString((Convert.ToDouble(e.Item.Cells(0).Text) / Convert.ToDouble(e.Item.Cells(1).Text)) + "%"
End Sub

In the aspx file:

<form id="Form1" method="post" runat="server">
      <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 27px; POSITION: absolute; TOP: 32px" runat="server"          Height="223px"                                  Width="326px" AutoGenerateColumns="False" OnItemDataBound="OnItemBound">
                        <Columns>
                              <asp:ButtonColumn Text="Country_ID" DataTextField="Country_ID" HeaderText="Country_ID" CommandName="BtnTest"></asp:ButtonColumn>
                              <asp:BoundColumn DataField="Country" HeaderText="Country"></asp:BoundColumn>
                              <asp:BoundColumn HeaderText="Pourcent"></asp:BoundColumn>
                        </Columns>
                  </asp:datagrid></form>


See the OnItemDataBound? its the name of my sub
Thats mean that for each line i do the operation.

So it will be executed for each record.

Simple hehe!!

Have fun! and take care!
0
 

Author Comment

by:dkilby
ID: 10898168
thanks for the help
0
 
LVL 5

Expert Comment

by:Higor
ID: 10899080
No problem ;)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now