SIGN UP MEMBER LOGIN:    
ARTICLE

Add Column Dynamically to DataGrid to Display Calculation

Posted by Sushila Patel Articles | WebForms Controls May 08, 2003
In this article we'll see how to create a DataGrid column which displays the calculation of more than one field in the database table. The calculation could be addition, multiplication or even filtering rows.
Reader Level:

Introduction:

In this article we'll see how to create  a Column which displays the calculation of more than one field in the database table. The calculation could be addition, multiplication or even filtering rows.

We'll consider the Northwind Database and Products Table.We'll multiply the UnitPrice to UnitsInStock to be displayed in the new column Total.

Solution:

Create a webform with a DataGrid control. We'll add the BoundColumn tag to  display column Total.

<asp:Datagrid id= "Datagrid1" AutoGenerateColumns= false style="Z-INDEX: 101; LEFT: 6px; POSITION: absolute; TOP: 14px" runat="server"><Columns>
<
asp:BoundColumn DataField="Productid" HeaderText="ProductiD">
</
asp:BoundColumn>
<
asp:BoundColumn DataField="ProductName" HeaderText="ProductName">
</
asp:BoundColumn>
<
asp:BoundColumn DataField="Total" HeaderText="Total" ataFormatString="{0:c}">
</
asp:BoundColumn>
</
Columns>
</
asp:DataGrid> 
 
In the code behind write the code which calculates Total =UnitPrice * UnitsInStock. To achieve this we  have made use of the Expression Property of the DataColumn.

The Expression Property : Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column. In our case we are using it to create calculated column.

Here goes the code:

Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to
initializethepageheremyconnection=NewSqlConnection
"Server=localhost;uid=sa;password=;database=northwind;")
myda =
New SqlDataAdapter("Select * from Products", myconnection)
ds =
New DataSet()
myda.Fill(ds, "AllTables")
Dim dc As DataColumn
dc =
New DataColumn("Total", Type.GetType("System.Double"))
dc.Expression = "UnitPrice * UnitsInStock"
ds.Tables(0).Columns.Add(dc)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

Login to add your contents and source code to this article
share this article :
post comment
 

what happens when i need a more advanced expresion (e.g. a Function in my project)

Posted by Marco Ruiz Jul 06, 2007

what happens when i need a more advanced expresion (e.g. a Function in my project)

Posted by Marco Ruiz Jul 06, 2007
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor