How to subtract two numeric columns(bound) of a Datagrid and display the result in the third column?

Hi Everyone,

I  have two numeric columns(databound-coming from SQL database) in my datagrid, 'PromoPrice' & 'CostPrice'. I need to calculate 'PromoPrice- CostPrice' and display the result in the third datagrid column.

i.e.: column2-column1 = column3

Any help anyone...

Many Thanks in advance,

Nico.
0
Nicok
10/3/2005 6:14:15 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

6 Replies
888 Views

Similar Articles

[PageSpeed] 46

In your query do this:
Select .... , (PromoPrice - CostPrice) AS Answer, .....
FROM yourTable

Keep in mind you can do math functions in queries.




Darmark

Mark as Answer, if this reply answers your post.
0
Darmark
10/3/2005 7:05:10 PM
Hi Darmark, thanks for your fast answer.

The fact is I'm using a single "select function" from webmatrix with one querie :

Function MyQueryMethod() As System.Data.DataSet
Dim connectionString As String = "server='prsasappweb'; user id=' '; password=' '; database='premier'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [PromoConfig].* FROM [PromoConfig]" Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.Sql
DataAdapter dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.
DataSet dataAdapter.Fill(dataSet)
Return dataSet
End Function 

Sub Page_Load (sender As Object, e As EventArgs)
DataGrid1.DataSource = MyQueryMethod()
DataGrid1.DataBind()
End Sub

This is the code of my Datagrid :

<asp:DataGrid id="DataGrid1" runat="server" BorderColor="White" DataMember="Table" BackColor="White" CellPadding="3" DataKeyField="IdPromoConfig" BorderWidth="2px" BorderStyle="Ridge" DataSource="<%# SqlDataSourceControl1 %>" AutoGenerateColumns="False" GridLines="None" CellSpacing="1">

                <FooterStyle forecolor="Black" backcolor="#C6C3C6"></FooterStyle>

                <HeaderStyle font-bold="True" forecolor="#E7E7FF" backcolor="#4A3C8C"></HeaderStyle>

                <PagerStyle horizontalalign="Right" forecolor="Black" backcolor="#C6C3C6"></PagerStyle>

                <SelectedItemStyle font-bold="True" forecolor="White" backcolor="#9471DE"></SelectedItemStyle>

                <ItemStyle forecolor="Black" backcolor="#DEDFDE"></ItemStyle>

                <Columns>

                    <asp:BoundColumn DataField="PrixPromo1" SortExpression="PrixPromo1" HeaderText="PrixPromo1"></asp:BoundColumn>

                    <asp:BoundColumn DataField="PrixCost1" SortExpression="PrixCost1" HeaderText="PrixCost1"></asp:BoundColumn>

                </Columns>

  </asp:DataGrid>

Is there a way to put the Querrie in an other "BoundColumn" or "TemplateColum" (Directly in the Datagrid) ?

Thanks again,

Nico

0
Nicok
10/3/2005 7:35:13 PM
Hi, what the Darmark meant was in your Sql statement

Dim queryString As String = "SELECT [PromoConfig].*,  (PromoPrice-CostPrice) AS Answer  FROM [PromoConfig]"

Then, in your <asp:DataGrid>

<Columns>
      <asp:BoundColumn DataField="PrixPromo1" SortExpression="PrixPromo1" HeaderText="PrixPromo1"></asp:BoundColumn>

      <asp:BoundColumn DataField="PrixCost1" SortExpression="PrixCost1" HeaderText="PrixCost1"></asp:BoundColumn>

      <asp:BoundColumn DataField="Answer" HeaderText="Profit ($)" ............/>
</Columns>



Hope this helps...


Alvin Chooi
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
0
alvinz_c
10/4/2005 3:32:35 AM
Hi alvinz,

Maybe I made a mistake but the result of the calcul doesn't display in the column "Profit ($)".

I did exactly like you tell me to do :

Dim queryString As String = "SELECT [PromoConfig].*, (PrixPromo1-PrixCost1) AS Answer FROM [PromoConfig]"

Then, in my <asp:DataGrid>

<Columns>

      <asp:BoundColumn DataField="PrixPromo1" HeaderText="PrixPromo1"></asp:BoundColumn>

      <asp:BoundColumn DataField="PrixCost1" HeaderText="PrixCost1"></asp:BoundColumn>

      <asp:BoundColumn DataField="Answer" HeaderText="Profit ($)"></asp:BoundColumn>

</Columns>

My fields (PrixPromo1,PrixCost1,Answer) is "Numeric". Is that right ?

Thanks for your help,

Nico

0
Nicok
10/4/2005 6:02:11 PM
That's working well now!

Thanks a lot for your Help !

Nico
0
Nicok
10/5/2005 9:53:14 AM
Glad that can give you a helping hand here. :)
Alvin Chooi
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
0
alvinz_c
10/5/2005 11:03:27 AM
Reply:

Similar Artilces:

How to subtract two date columns(bound) of a Datagrid and display the result in the third column?
Hi Everyone,I  have two date columns(databound-coming from SQL database) in my datagrid, 'start date' & 'end date'. I need to calculate number of days with 'end date- start date' and display the result in the third datagrid column. i.e.: column2-column1 = column3Any help anyone?...ASAP please got a deadline to meet :) (SOS!!!)Many Thanks in advance,Arty.Aartee...we live more than once! Hi, there are several ways of doing it. One of them is creating a label in the TemplateColumn, use the Helper function to calculate the number of days.<ItemTemplate>   <...

display the sum of two datagrid columns into third column
hi... there..     i have a datagrid with columns named Price ,Quantity and total. now in the total column i  wanna display the prices which comes by multiplyiing the price and quantity (like  total=Price * quantity). I'm workin in asp.net +vb.net.                     may i knw hw can i do this.. plz write the code.. thanks.         Hi, do like this..suppose u have,        string ProductPrice = ...

Multiply two columns and store the result into third column
Hi,  I am new to asp.net world. I am having 1 problem in a gridview.   There are 3 template fields in my gridview namely Length , Width and Area. Length n Width contain textboxes and Area is having label. Problem is as soon as you enter the values in textboxes, the area should be calculated and reflected immediately in the area field of that row. I am using RowDataBound event gridview. But area is not getting update dynamically after changing the textfields.  Code i m using is: protected void gvArea_RowDataBound(object sender, GridViewRowEventArgs ...

column header to span two columns in a datagrid display
Is it possible for a column header to span two columns? If so how would this be done? Thanks In the ItemDataBound (or ItemCreated) check if Item is of type Header and if so - define the colspan property of the Cell object which you want to define the Colspan for and delete the next Cell from hte DataGridItem CheersBug [MCSD] Is it possible to span header column in design view only. using some kind of CSS. I'm using ASP,NET 3.5 and GridView...

How to display column total of numeric columns in the datagrid footer
I able to connect Datagrid control to ADO.Net for pulling data. But I can not able to display total of a numeric field column in the Datagrid footer. Is it possible? How? Please tell me any one. Hi, please read up Displaying Column Summary Information in the Footer and HOW TO: Create a Summary Row for a DataGrid in ASP.NETHope this helps..Alvin ChooiMicrosoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia Blog : http://alvinzc.blogspot.com...

Setting Datagrid Column to invisible when Column is an AutoGeneratedColumn NOT a bound Column
Hi all, Just like the title says, I'm trying to set a column to invisible but I'm unable to do so. If I bound the column I can used the code: me.datagrid.Column.Item(1).Visible = False But since I'm wanting the column NOT to be bound (there are a number of sql scripts that populate the same datagrid so I cannot bound all the columns since the columns are different for each script etc etc etc) how do I set an AutoGeneratedColumn to invisible?? Please help! Andrewwww.codingmayhem.com - VB.NET, C#, Silverlight, Javascript, HTML, CSS, T-SQL, MySQL and SQLitewww.headshot...

Datagrid Bound Column needs to be bound to 2 columns?a valu
Hi I have a datagrid with dynamically bounded columns. One column has to display a value that will come from 2 columns in the database. Is there a way to do this? the code I have for the moment is: 'Add columns 'Code Dim col1 As New BoundColumn col1.HeaderText = cLang.read("code") col1.DataField = "Patient_Code" 'this needs to be 2 database columns!! dtaSearchresult.Columns.Add(col1) .. .. .. .. Thx alot! Combine the two columns in your SELECT statement eg SELECT <PatientCol1>+<PatientCol2> AS Patient_Code FR...

Addition of two Columns in third Column.
Hi Guys, I got stucked in my project and i need your help. I have a data grid with data from sql 2005. I have got two amount columns - Amount1 & Amount2 with values from database. I want the addition of both in Total column (type: template) which I added in data grid view I also want the grand total of Total column in the bottom. Pls help  >I have got two amount columns - Amount1 & Amount2 with values from databaseWhy not change the select? ChangeSELECT  Amount1, Amount2 FROM FREDto SELECT  Amount1, Amount2, (Amount1 + Amount2) AS Amount3 FROM FRED...

Compare Two columns and update the Third Column
 Hi,I have three columns and want to update the third column based on the comparison of first two columns in the same table.For instance:Col1 - val1, val2, val3, val4, val5......Col2 - NULL, NULL, val1, NULL, val2....Col3 - TRUE, TRUE, FALSE, FALSE, FALSE....Thanks for any help. When you define a column, you can define it as a "computed column". Make col3 a computed column and fill in the comparison expression.    Dear David,Thanks for your reply. Actually I have table with more then 7000 records in it. and all the columns are already defined. I was lookin...

How to display the sum of a column at the footor of the datagrid column
HI, Any idea how I can display the Sum of any column at the Footor of that Column. HighOnCodingWanna get high! Hi, Take a look @ Displaying Column Summary Information in the Footer HTH K B ShajudeenAreYaar.comDubai, United Arab Emirates....

DataGrid: Bound Column vs Template Column
I have a datagrid with a bound column and a template column as given below.When I scan through the grid, I get the value of the first column (bound column). However, I cannot get the value of the second column (Template Column). I can see the data in both columns though.////////////////////////////////////////////////////////////////////////////////////<AlternatingItemStyle BackColor="#e0e0e0"></AlternatingItemStyle><Columns><asp:BoundColumn DataField="Facility" HeaderText="Facility"> </asp:BoundColumn><asp:TemplateColumn HeaderText="Facility2"><ItemSty...

datagrid columns bound to columns from different tables
Hi, I have a dataset with multiple tables. Is there a way to bind each of the datagrid's columns to a column from a different tables? Thanks in advance! BR The bottom line, yes you can, but it would take some custom methods getting called from a template column and some other fancy tricks. But, the datagrid is designed and works best when it's given it's one datasource. If you need data from different columns in different tables, I would make a custom datatable that reflects the columns you need and use that custom datatable has the datagrid's datasource. Don't think of t...

how to split the content of one column in the objectdatasource into two parts and let them display in two columns in the GridView?
i combine the title and the body of one article and store it in one column in the database,but i want to show the title and body seperately in the GridView,how should i achieve this goal?thank you so much for your advice!!! you can write the logic of splitting the column into two (title and body) in the query itself by which you are filling the datasource..Thanx, [KaushaL] || BloG || Profile || Microsoft MVP"I would love to change the world, but they won’t give me the source code"Don't forget to click "Mark as Answer" on the post that helped you.This credits that me...

how do i get the column i want if result has two columns with the same name?
 let's say my select command returns two columns with the same name "Id".(those two columns are from different tables) so would it be possible to get the data from the second column if i don't use the index? thanks in advance   Try to use alias: SELECT table1.id as id1, table2.id as id2, othercolumn from......Limno...

Web resources about - How to subtract two numeric columns(bound) of a Datagrid and display the result in the third column? - asp.net.presentation-controls

FlowPlus: Add. Subtract. Enter the Flow. on the App Store on iTunes
Get FlowPlus: Add. Subtract. Enter the Flow. on the App Store. See screenshots and ratings, and read customer reviews.

Just subtract gravity: How astronaut Chris Hadfield has made the mundane cool
IF you look up at the heavens once every 92 minutes, you may come under the gaze of the man who has made the mundane cool.

Fruit Ninja: Math Master Is An Educational Take On A Popular Game That Adds A $5 Price Tag And Subtracts ...
You don't need an introduction to Fruit Ninja. You're probably playing it right now. It has attracted millions of players over the years, partly ...

Slide, add, and subtract to defend your base in Cubic!, a new strategic puzzle game
Cubic! Threes! Noda Slide, add, and subtract to defend your base in Cubic!, a new strategic puzzle game is a story by AppAdvice.

Price Drop: FlowPlus: Add. Subtract. Enter the Flow.
FlowPlus: Add. Subtract. Enter the Flow. 1.0.2 Device: iOS Universal Category: Education Price: Free, Version: 1.0.2 ( iTunes ) Description: ...

Astros Needed To Subtract First
Astros Needed To Subtract First

Feedly is a mess! New updates subtract features, erase data
The Google Reader shutdown looms large on our horizon, and it seems no last minute reprieve from the governor will be coming before that switch ...

Bing Adds Hardware and Curriculum for Schools, Subtracts Ads
Today marks the official launch of Bing for Schools, a new initiative designed to improve digital literacy for students by putting technology ...

Consumers Cut Back on Toilet Paper, Pampers, Huggies; Payroll Tax Bite to Subtract .8% from GDP
... not to buy their usual Charmin toilet paper and Purina One dog food, choosing less-expensive versions instead. Payroll Tax Bite to Subtract ...

Check Out The Growth Of America's Economy Once You Subtract The Growth Of America's Debt...
... debt," but it's really only $100 that has been borrowed. But still. Just for fun, here's what our economic growth looks like if you subtract ...

Resources last updated: 1/15/2016 11:20:31 PM