I have a GridView in ASP which I use for displaying products in a shopping cart.
I set the source of GridView the ShopDataSet and the columns Name, Price and Quantity are filled automatically.
What I want to achieve is a new column, added by me, which displays the cost of each row i.e. Cost = price * quantity;
How can I do this programatically, without executing a new query on the database?
I have to say that on Price and Quantity I set format like "${0}" for Price and "{0} piece(s)" for Quantity.
You should populate your datasource programmatically.
Get the results in a SqlDataReader and populate a DataTable with the results of the SqlDataReader.
Now create a new DataTable with columns that match the output that you want including Cost in your case. Now iterate through each row of the 1st DataTable and add rows to the new DataTable with the cost calculation.
For Example,
Now create your new table
Now set the DataSource of your grid to NewTable