Solved

asp.net + datagrid + division of two colums

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

11 Experts available now in Live!

Get 1:1 Help Now