ASP.Net DataColumn Expression Property
ASP.Net DataColumn Expression property provides the functionality to calculate the values in a column or create an aggregate column. Calculating a column value using Expression property of a DataColumn means evaluation of a new value in the column using mathematical operations such as addition, subtraction, multiplication or division etc. You can also use SQL aggregate functions such as avg, sum, min, max, count etc to calculate the value targeted on any column or group of columns. ASP.Net DataColumn accepts the Expression property value in two ways. First way is while initialization of DataColumn, an overloaded constructor accepts the Expression string value as 3rd parameter that creates a new data column whose value evaluates on the basis of targeting column in the expression. Second way is after initialization step you can access the Expression property e.g.: myDataColumn.Expression
C# Code for ASP.Net DataColumn Expression Property
// Initialize a DataTable
DataTable myDataTable = new DataTable();
// Initialize DataColumn
DataColumn myDataColumn = new DataColumn();
// Add First DataColumn
// AllowDBNull property
myDataColumn.AllowDBNull = false;
// set AutoIncrement property to true
myDataColumn.AutoIncrement = true;
// set AutoIncrementSeed property equal to 1
myDataColumn.AutoIncrementSeed = 1;
// set AutoIncrementStep property equal to 1
myDataColumn.AutoIncrementStep = 1;
// set ColumnName property to specify the column name
myDataColumn.ColumnName = "auto_ID";
// set DataType property of the column as Integer
myDataColumn.DataType = System.Type.GetType( "System.Int32" );
// set Unique property of DataColumn to true to allow unqiue value for this column in each row
myDataColumn.Unique = true;
// Add and Create a first DataColumn
myDataTable.Columns.Add(myDataColumn);
// Add second DataColumn
// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = new DataColumn();
myDataColumn.ColumnName = "Unit_Name";
// set DataType property of the column as String
myDataColumn.DataType = System.Type.GetType( "System.String" );
// Add and Create a Second DataColumn
myDataTable.Columns.Add(myDataColumn);
// Add third DataColumn
// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = new DataColumn();
myDataColumn.ColumnName = "Unit_Price";
// set DataType property of the column as Double
myDataColumn.DataType = System.Type.GetType( "System.Double" );
// Add and Create a Third DataColumn
myDataTable.Columns.Add(myDataColumn);
// Add fourth DataColumn
// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = new DataColumn();
myDataColumn.ColumnName = "Units_In_Pack";
// set DataType property of the column as Integer
myDataColumn.DataType = System.Type.GetType( "System.Int32" );
// Add and Create a fourth DataColumn
myDataTable.Columns.Add(myDataColumn);
// Add fifth DataColumn
// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = new DataColumn("Net_Price", System.Type.GetType( "System.Double" ), "Units_In_Pack * Unit_Price");
// Add and Create a fifth DataColumn
myDataTable.Columns.Add(myDataColumn);
// create a new row using NewRow() function of DataTable.
// dataRow object will inherit the schema of myDataTable to create a new row
DataRow dataRow = myDataTable.NewRow();
dataRow["Unit_Name"] = "ASP.Net 2.0 [3 in 1 Pack]";
dataRow["Unit_Price"] = "35.19";
dataRow["Units_In_Pack"] = "3";
// add new data row to the data table.
myDataTable.Rows.Add(dataRow);
// similarly adds the second row to the DataTable
dataRow = myDataTable.NewRow();
dataRow["Unit_Name"] = "ASP.Net 2.0 with AJAX [4 in 1 Pack]";
dataRow["Unit_Price"] = "39.39";
dataRow["Units_In_Pack"] = "4";
myDataTable.Rows.Add(dataRow);
// similarly adds the third row to the DataTable
dataRow = myDataTable.NewRow();
dataRow["Unit_Name"] = "ASP.Net 3.5 with AJAX [4 in 1 Pack]";
dataRow["Unit_Price"] = "42.19";
dataRow["Units_In_Pack"] = "4";
myDataTable.Rows.Add(dataRow);
myDataTable.AcceptChanges();
GridView1.DataSource = myDataTable;
GridView1.DataBind();
In the above C# code you can see that how to create table columns in different ways using different set of properties. See the fifth DataColumn i.e. "Net_Price" in the above C# code, it has been created using single line of code by passing three parameters during its initialization. 3rd parameter has been passed as expression to calculate the value for the DataColumn automatically. You can also use the following style to set the value for expression property of ASP. Net DataColumn:
// initialize a new instance of DataColumn to add another column with different properties. myDataColumn = newDataColumn(); myDataColumn.ColumnName="Net_Price"; myDataColumn.DataType = System.Type.GetType( "System.Double" ); myDataColumn.Expression = "Units_In_Pack * Unit_Price"; // Add and Create a DataColumn myDataTable.Columns.Add(myDataColumn);
HTML Code for ASP.Net GridView Bound to DataTable
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false"
ShowFooter="true">
<Columns>
<asp:BoundField
DataField="auto_ID"
HeaderText="Auto ID" />
<asp:BoundField
DataField="Unit_Name"
HeaderText="Unit Name" />
<asp:BoundField
DataField="Units_In_Pack"
HeaderText="Unit in Pack" />
<asp:BoundField
DataField="Unit_Price"
HeaderText="Unit Price"
DataFormatString="{0:C}"
HtmlEncode="false" />
<asp:BoundField
DataField="Net_Price"
HeaderText="Net Price"
DataFormatString="{0:C}"
HtmlEncode="false" />
</Columns>
</asp:GridView>
Output
Next learn how to use ASP.Net DataTable Compute Column Sum using C# to display the sum of column values.

I am using below code is working.it is saying that "Cannot perform '*' operation on System.String and System.String."
DataColumn totalColumn = new DataColumn();
totalColumn.ColumnName = "Result";
//totalColumn.DataType = System.Type.GetType("System.String");
totalColumn.Expression = "growth_ran * risk_rank";
// Add columns to DataTable.
dt.Columns.Add(totalColumn);
DataView view = new DataView(dt);
GridView1.DataSource = view;
GridView1.DataBind();
I am waiting for great response.
Thanks
Venkat