Solved

asp.net + datagrid + division of two colums

Posted on 2004-04-22
5
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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 …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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