Solved

asp.net + datagrid + division of two colums

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

749 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