Click here to Skip to main content
14,773,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can we manipulate data in grid view columns before displaying
i know inside a template field we can do some fancy stuff but can we (say) add data from two columns b4 displaying under a single column
Text='<%# Bind("brandname") %>'

can we do any calculation inside `<%# %>` like adding two columns together assuming they are integers

Sure you can. But you need to be careful enough to do so. Because coding in design mode is not as convenient as server side code. For example
<asp:TextBox runat="server" ID="tbDiscount"  readonly="true" text='<%# 0.1 * DataBinder.Eval(Container.DataItem,"UnitPrice") %>'/>

will calculate a discount and display to discount textbox.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="PracticeWeb.WebForm5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
            <asp:BoundField DataField = "Name" HeaderText = "Name" />
            <asp:BoundField DataField = "Price" HeaderText = "Price" />
            <asp:BoundField DataField = "Tax" HeaderText = "Tax" />
            <asp:BoundField HeaderText = "Total" />


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("YYY", 30, 15);
            _dt.Rows.Add("ZZZ", 40, 20);
            GridView1.DataSource = _dt;
        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.

With DataGrid2
.Visible = True
.DataSource = MyBodyTable
.SelectedIndex = MyBodyTable.Rows.Count - 1
Dim i As Integer = 0
For i = 0 To .Items.Count - 1
.Items(i).Cells(1).HorizontalAlign = HorizontalAlign.Right
.Items(i).Cells(3).HorizontalAlign = HorizontalAlign.Right
.Items(i).Cells(5).HorizontalAlign = HorizontalAlign.Right
End With
DataGrid2.Items(2).Font.Name = "Arial"
DataGrid2.Items(2).Font.Size = Unit.Point(14).Value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900