Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2006, 05:52 PM
Registered User
 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add a Column to a DataGrid

Is it possible to add a column to a DataGrid. Say for instance I have two columns Quantity and Cost that I would like to multiply the two to get total and display within the DataGrid.

In the actual data I'm using (its for a budget preparation project), I would like to generate 3 columns in addition to the ones already sent back from the database, each with several calculations on the data within each row.

I know I can create a stored procedure that will run the calculations but I would like to know if anyone knows if its possible within ASP.NET's DataGrid or 2.0's server controls.

John
 
Old January 6th, 2006, 10:08 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You have a couple options:

Use a data structure that you can easily modify such as a DataTable. After loading the data, you could add columns, spin thru the data, do the calculations and put the results into the new columns. Then bind the whole lot to the datagrid.

Alternatively, you could just add some extra template columns to the datagrid, and write databinding syntax that does the calculations right in the control markup.

-Peter
 
Old January 6th, 2006, 01:50 PM
Registered User
 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks planoie!

Ok so I'll explore use of the DataTable.

On the possible 2nd option, would you have any samples or can you write a simple example of databinding syntax that does calculations in the control markup? Is this solely a 2.0 option?

John

 
Old January 13th, 2006, 05:25 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

<%# 1 + 1 %>

Seriously though, you can put pratically anything inside data binding syntax. If your data item were numeric:

<%# Container.DataItem * 12 %>

If it is a string:

<%# Container.DataItem + "Hello World" %>

and so on. To calculate a result of some fields as another example then this might work:

<#% DataBinder.Eval(Container.DataItem, "Qty") * DataBinder.Eval(Container.DataItem, "PricePerItem") %>

-Peter
 
Old January 13th, 2006, 05:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Just don't forget that DataBinder.Eval uses reflection so it's always slower than strongly typed access. Where appropriate, cast your objects to proper types before you access its properties.

Check out this link for some details:

http://www.dotnetslackers.com/_NET/r...n_ASP_NET.aspx

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 15th, 2006, 01:06 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by infernokodiak
 Is it possible to add a column to a DataGrid. Say for instance I have two columns Quantity and Cost that I would like to multiply the two to get total and display within the DataGrid.
 its possible to de them in code-behind...
Code:
DataColumn dtc=new DataColumn("Sum",typeof(int));
Code:
   dtc2.Expression="Quantity * Cost";
   dataTable.Columns.Add(dtc);


_____________
Mehdi.
software student.
 
Old January 15th, 2006, 01:11 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by infernokodiak
 Is it possible to add a column to a DataGrid. Say for instance I have two columns Quantity and Cost that I would like to multiply the two to get total and display within the DataGrid.
 you can do it in code-behind
Code:
DataColumn dtc=new DataColumn("Sum",typeof(int));
Code:
   dtc.Expression="Quantity * Cost";
   dataTable.Columns.Add(dtc);


_____________
Mehdi.
software student.
 
Old January 18th, 2006, 04:06 PM
Registered User
 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Appreciate all the input. Learning to work with the DataGrid has been a fun experience! though frustrating at times. :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add a new column into gridview? Bryan Stanley ASP.NET 2.0 Basics 0 September 12th, 2007 10:49 PM
Add 1 to next column deontae45 SQL Language 2 December 18th, 2006 01:42 PM
add column if not exist Dj Kat MySQL 1 February 16th, 2006 01:57 PM
how to add new column to recordset?? huela Access VBA 0 October 13th, 2004 01:22 PM
add column (C#) kobystud C# 1 July 21st, 2004 10:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.