โค้ด: เลือกทั้งหมด
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RPT.aspx.cs" Inherits="RPT" %>
<!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>Untitled</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="3px" CellPadding="2" ForeColor="Black" GridLines="None" BorderStyle="None"
CellSpacing="2" Font-Names="Verdana" ShowFooter="true" Font-Size="8pt" AllowPaging="false"
OnRowCreated="GridView1_RowCreated" >
<FooterStyle BackColor="Tan" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
</form>
</body>
ซึ่งจะสร้าง custom column ให้กับ Gridview ก่อนที่จะ render ออกสู่หน้าจอ
โค้ด: เลือกทั้งหมด
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTabel dt = this.LoadReport();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//Build custom header.
GridView oGridView = (GridView)sender;
GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell oTableCell = new TableCell();
oTableCell.Text = "Department";
oTableCell.ColumnSpan = 2; [color=#FF0000]// จำนวน column ที่จะถูกครอบด้วยคำว่า Department (ซึ่งก็คือ คอมลัมน์ที่ 0 และ คอลัมน์ที่ 1)[/color]
oGridViewRow.Cells.Add(oTableCell);
oTableCell = new TableCell();
oTableCell.Text = "Employee";
oTableCell.ColumnSpan = 3; [color=#FF0000]// จำนวน column ที่จะถูกครอบด้วยคำว่า Employee (ซึ่งก็คือ คอมลัมน์ที่ 2 คอลัมน์ที่ 3 และ คอลัมน์ที่ 4)[/color]
oTableCell.HorizontalAlign = HorizontalAlign.Center;
oGridViewRow.Cells.Add(oTableCell);
oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);
}
}