| | |
Datagrid
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
•
•
Join Date: Mar 2005
Posts: 10
Reputation:
Solved Threads: 0
I have a database with two tables customers & sales what i would like to do is in the sales datagrid is to have a total column that calculates the quantity*price columns and shows the result in the total column i don't know if i need to do this with unbound column or with a databound column i looked at the msdn2 on the mirosoft site and just got more confused hope someone can point out steps to do this. thankyou Barryt
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
you create an unbound column in the sales and use the Expression property whose value is quantity*price (assuming those are the two column names). Only create bound columns if you want data saved in the database or pulled from the database (you could have created the column in your query and pulled it into your grid but when a new row is added the total wouldnt automatically update - whereas it does with an expression).
•
•
Join Date: Mar 2005
Posts: 10
Reputation:
Solved Threads: 0
Hi F1 fan
I added a unbound column in the columns collection editor with the following code in the sales cs but i am not getting anything in the datagrid if you have time to look at this you might tell what i am doing wrong heres the code.
private void CalcColumn()
{
DataColumn Column1 = new DataColumn("Total",typeof(string));
Column1.Expression = "Quantity * Price";
database1Dataset.Columns.Add(Column1);
}
Thanks Barry
I added a unbound column in the columns collection editor with the following code in the sales cs but i am not getting anything in the datagrid if you have time to look at this you might tell what i am doing wrong heres the code.
private void CalcColumn()
{
DataColumn Column1 = new DataColumn("Total",typeof(string));
Column1.Expression = "Quantity * Price";
database1Dataset.Columns.Add(Column1);
}
Thanks Barry
•
•
Join Date: Mar 2005
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by f1 fan
database1Dataset.Columns.Add(Column1);
that line is incorrect
Cannot add a column to a dataset... need
database1dataset.Tables["sales"].Columns.Add(Column1);
if sales was the table name
I changed the code but still not showing anything in the total column any other ideas that might solve this problem. thanks Barry
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
when do you call CalcColumn()?
You havent quite grasped the concept which is where you are going wrong but thats why we are here
Are you using a strong typed dataset to get your data? You dont want to add the unbound column to the grid in a grid editor (this isnt an unbound grid column its an unbound datacolumn - there is a big difference in the two).
Wherever you get your dataset from... before you pass it to the grid to bind it, thats where you need to put those lines of code.
It will work, its just you arent calling it at the correct time (if at all).
So you need to
1. Get your dataset from the database
2. Add the unbound column with the expression to the datatable
3. Bind it to the grid.
If you have a strong typed dataset then you can add the column there and even set the expression there. IF not then you have to use those few lines of code.
Try and grasp this concept (unfortunately they are similar names so everyone gets confused in the beginning). You have a database which has tables and columns. You have an ado.net dataset which has tables and columns (these can map one to one but do not have to! thats very important.. it was done deliberately to give us more power... old ado before ado.net would not let us split the two). You have a grid that has columns. All columns are different entities and although you can map or bind them they can exist without each other.
Hope i might have cleared things up a wee bit
You havent quite grasped the concept which is where you are going wrong but thats why we are here

Are you using a strong typed dataset to get your data? You dont want to add the unbound column to the grid in a grid editor (this isnt an unbound grid column its an unbound datacolumn - there is a big difference in the two).
Wherever you get your dataset from... before you pass it to the grid to bind it, thats where you need to put those lines of code.
It will work, its just you arent calling it at the correct time (if at all).
So you need to
1. Get your dataset from the database
2. Add the unbound column with the expression to the datatable
3. Bind it to the grid.
If you have a strong typed dataset then you can add the column there and even set the expression there. IF not then you have to use those few lines of code.
Try and grasp this concept (unfortunately they are similar names so everyone gets confused in the beginning). You have a database which has tables and columns. You have an ado.net dataset which has tables and columns (these can map one to one but do not have to! thats very important.. it was done deliberately to give us more power... old ado before ado.net would not let us split the two). You have a grid that has columns. All columns are different entities and although you can map or bind them they can exist without each other.
Hope i might have cleared things up a wee bit
do this with sql and it will save you a lot of trouble, here is a quick example assuming the table has a column called price and one called quantity
SELECT Price, Quantity, Price * Quantity AS Total FROM myTable
the server will preform the multiplication for you add assign it the name "Total"
---
if your looking at getting the total from all the columns in the table no just a single row you could use this aggregate function
SELECT COUNT(*) AS NumberOfInvoices, SUM(Price * Quantity) AS TableTotal FROM myTable
count gets the number off rows included, and sum is a continuing addition value of each rows values, as evaulated.
SELECT Price, Quantity, Price * Quantity AS Total FROM myTable
the server will preform the multiplication for you add assign it the name "Total"
---
if your looking at getting the total from all the columns in the table no just a single row you could use this aggregate function
SELECT COUNT(*) AS NumberOfInvoices, SUM(Price * Quantity) AS TableTotal FROM myTable
count gets the number off rows included, and sum is a continuing addition value of each rows values, as evaulated.
•
•
•
•
Originally Posted by barry t
Hi plazmo
Thankyou for your reply it works really well just one question if i add two more columns amountpayed & balance to my sales table is it possible to have two querys for the same row to show quantity*price=total-amountpayed=balance.
thanks Barry
try this, i dont know if this works tho
SELECT price, quantity, quantity*price AS Total, Total-amountpayed AS Balance FROM myTable
if that doesnt work just replace Total with the formula for total
SELECT price, quantity, quantity*price AS Total, quantity*price-amountpayed AS Balance FROM myTable
![]() |
Similar Threads
- binding a datagrid to a datareader (ASP.NET)
- Printing the datagrid control (ASP.NET)
- How to sort the rows in the datagrid and update it successfully? (VB.NET)
- Use of DataGrid (ASP.NET)
- Remove link frame around picture in Datagrid (ASP.NET)
- Linking to E-mail through datagrid (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Custom Control Button with different regions
- Next Thread: C# VS 2005 - SQL Query Parameters to an ODBC DataSource
| Thread Tools | Search this Thread |
.net 2007 access algorithm appportability array bitmap box c# camera check checkbox client combobox control conversion cryptographyc#winformsencryption cs4 csharp customactions database datagrid datagridview dataset datastructure date datetime degrees disabled displayingopenforms draganddrop drawing encryption enum eventcloseformc# excel file form format forms ftp function gdi+ gis image index index-error input install java list listview load math mathematics mouseclick mysql operator path photoshop picturebox pixelinversion pixels post prime programming radians regex remoting richtextbox rotation search security server setup sleep socket sql statistics stream string table text textbox thread time timer totaldays update user usercontrol validation view visual visualstudio webbrowser winforms wordautomation wpf xml





