C# Gridview มี Header หลายๆ level (Merge Header)

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
jataz2
PHP Super Member
PHP Super Member
โพสต์: 275
ลงทะเบียนเมื่อ: 22/02/2011 11:48 am

C# Gridview มี Header หลายๆ level (Merge Header)

โพสต์ที่ยังไม่ได้อ่าน โดย jataz2 »

ดังรูปคงพอเห็น idea เเล้วนะครับ ที่นี่เข้าไปในวิว Design แล้วเพิ่ม html ดังนี้ครับ

โค้ด: เลือกทั้งหมด

<%@ 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>
ในหน้า .cs เพิ่ม code เหล่านี้เข้าไปครับ หลักๆจะอยู่ที่ OnRowCreated

ซึ่งจะสร้าง 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);
        }
    }
แนบไฟล์
Merge_Header.jpg
Merge_Header.jpg (22.99 KiB) Viewed 2772 times
ภาพประจำตัวสมาชิก
imsn
PHP Super Member
PHP Super Member
โพสต์: 375
ลงทะเบียนเมื่อ: 07/05/2010 12:58 pm
ติดต่อ:

Re: C# Gridview มี Header หลายๆ level (Merge Header)

โพสต์ที่ยังไม่ได้อ่าน โดย imsn »

แจ่มเลยครับ
แต่เดี๋ยวนี้ไม่ได้ใช้ ล่ะ asp
ช่วยตอบเพราะอยากโปรโหมทเว็บ คิคิคลิกเลย
หาเพื่อนฝรั่งคุย M ...
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 90