Binding more than one column as a single column to GridView
.ASPX Code:
========
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataGridTest.aspx.cs" Inherits="Ajaxapp.DataGridTest" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="grvtest" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label runat="server" ID="lblAddress" Text='<%#DataBinder.Eval(Container.DataItem,"addr1")+" "+DataBinder.Eval(Container.DataItem,"addr2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
.Cs Code:
=======
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace Ajaxapp
{
public partial class DataGridTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("addr1", typeof(string));
dt.Columns.Add("addr2", typeof(string));
DataRow drow = dt.NewRow();
drow["addr1"] = "Nerkundram";
drow["addr2"] = "Chennai";
DataRow drow1 = dt.NewRow();
drow1["addr1"] = "Secundrabad";
drow1["addr2"] = "AP";
DataRow drow2 = dt.NewRow();
drow2["addr1"] = "";
drow2["addr2"] = "Bangalore";
DataRow drow3 = dt.NewRow();
drow3["addr1"] = "Electronic City";
drow3["addr2"] = "Karnataka";
DataRow drow4 = dt.NewRow();
drow4["addr1"] = "Kadapa";
drow4["addr2"] = "Ap";
DataRow drow5 = dt.NewRow();
drow5["addr1"] = "Valchary";
drow5["addr2"] = "Chennai";
dt.Rows.Add(drow);
dt.Rows.Add(drow1);
dt.Rows.Add(drow2);
dt.Rows.Add(drow3);
dt.Rows.Add(drow4);
dt.Rows.Add(drow5);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
grvtest.DataSource = ds;
grvtest.DataBind();
}
}
}
Output:
No comments:
Post a Comment