Solved

asp.net + datagrid + division of two colums

Posted on 2004-04-22
5
405 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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