ASPX Page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="PracticeWeb.WebForm5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField = "Name" HeaderText = "Name" />
<asp:BoundField DataField = "Price" HeaderText = "Price" />
<asp:BoundField DataField = "Tax" HeaderText = "Tax" />
<asp:BoundField HeaderText = "Total" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
CodeBehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace PracticeWeb
{
public partial class WebForm5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable _dt = new DataTable();
_dt.Columns.Add("Name", typeof(string));
_dt.Columns.Add("Price", typeof(int));
_dt.Columns.Add("Tax", typeof(int));
_dt.Rows.Add("XXX",20,10);
_dt.Rows.Add("YYY", 30, 15);
_dt.Rows.Add("ZZZ", 40, 20);
GridView1.DataSource = _dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Text = Convert.ToString(Convert.ToInt16(e.Row.Cells[1].Text) + Convert.ToInt16(e.Row.Cells[2].Text));
}
}
}
}
You can do this on RowDataBound event of gridview.
++Subhendu