Click here to Skip to main content
15,886,026 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
Posted

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
C++
<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.
 
Share this answer
 
ASPX Page

XML
<%@ 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

C#
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
 
Share this answer
 
With DataGrid2
.Visible = True
.DataSource = MyBodyTable
.SelectedIndex = MyBodyTable.Rows.Count - 1
.DataBind()
'RIGHT ALIGN THE COLUMNS WHICH HAVE FIGURES
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
Next
End With
DataGrid2.Items(2).Font.Name = "Arial"
DataGrid2.Items(2).Font.Size = Unit.Point(14).Value
 
Share this answer
 

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