.NET GridView多層ヘッダー
.NET GridView多層ヘッダ実装方法
効果は図のようになります.
バックグラウンドコードファイル
データテーブルクラス
フロントソースファイル
効果は図のようになります.
バックグラウンドコードファイル
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEYOND;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BS bs = new BS();
gvBS.DataSource = bs.DataTableBS;
gvBS.DataBind();
}
protected void gvBS_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
#region----- -----
GridViewRow rh0 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
rh0.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
rh0.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
rh0.Font.Bold = true;
TableCellCollection c0 = e.Row.Cells;
TableCell hc = new TableCell();
hc.Text = "BS";
hc.RowSpan = 2;
rh0.Cells.Add(hc);
hc.HorizontalAlign = HorizontalAlign.Center;
rh0.Cells.Add(hc);
hc = new TableCell();
hc.Text = "BEYOND";
hc.ColumnSpan = 4;
hc.HorizontalAlign = HorizontalAlign.Center;
rh0.Cells.Add(hc);
hc = new TableCell();
hc.Text = " ";
hc.ColumnSpan = 2;
rh0.Cells.Add(hc);
hc.HorizontalAlign = HorizontalAlign.Center;
hc = new TableCell();
hc.Text = " ";
hc.RowSpan = 2;
rh0.Cells.Add(hc);
hc.HorizontalAlign = HorizontalAlign.Center;
rh0.Visible = true;
gvBS.Controls[0].Controls.AddAt(0, rh0);
#endregion
#region----- -----
GridViewRow rh1 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
rh1.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
rh1.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);
rh1.Font.Bold = true;
TableCellCollection c1 = e.Row.Cells;
TableCell hc1 = new TableCell();
hc1.Text = " ";
hc1.RowSpan = 1;
rh1.Cells.Add(hc1);
hc1.HorizontalAlign = HorizontalAlign.Center;
rh1.Cells.Add(hc1);
hc1 = new TableCell();
hc1.Text = " ";
hc1.RowSpan = 1;
hc1.HorizontalAlign = HorizontalAlign.Center;
rh1.Cells.Add(hc1);
hc1 = new TableCell();
hc1.Text = " ";
hc1.RowSpan = 1;
hc1.HorizontalAlign = HorizontalAlign.Center;
rh1.Cells.Add(hc1);
hc1 = new TableCell();
hc1.Text = " ";
hc1.RowSpan = 1;
rh1.Cells.Add(hc1);
hc1.HorizontalAlign = HorizontalAlign.Center;
hc1 = new TableCell();
hc1.Text = " ";
hc1.ColumnSpan = 1;
hc1.HorizontalAlign = HorizontalAlign.Center;
rh1.Cells.Add(hc1);
hc1 = new TableCell();
hc1.Text = " ";
hc1.ColumnSpan = 1;
hc1.HorizontalAlign = HorizontalAlign.Center;
rh1.Cells.Add(hc1);
rh1.Visible = true;
gvBS.Controls[0].Controls.AddAt(1, rh1);
#endregion
}
}
}
データテーブルクラス
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace BEYOND
{
/// <summary>
/// DataTableBS の の です
/// </summary>
public class BS
{
private DataTable dtBS;
public DataTable DataTableBS
{
get { return dtBS; }
set { dtBS = value; }
}
public BS()
{
//
// TODO: コンストラクタ ロジックをここに します
//
int len = 8;
DataRow dr;
string[] sData;
dtBS = new DataTable();
dtBS.Columns.Add("BS");
dtBS.Columns.Add("BEYOND01");
dtBS.Columns.Add("BEYOND02");
dtBS.Columns.Add("BEYOND03");
dtBS.Columns.Add("BEYOND04");
dtBS.Columns.Add("YuQuan01");
dtBS.Columns.Add("YuQuan02");
dtBS.Columns.Add("Dream");
sData = new string[len];
sData[0] = " ";
sData[1] = " ";
sData[2] = " ";
sData[3] = " ";
sData[4] = " ";
sData[5] = " ";
sData[6] = " ";
sData[7] = " ";
dr = dtBS.NewRow();
for (int i = 0; i < len; i++)
{
dr[i] = sData[i];
}
dtBS.Rows.Add(dr);
}
}
}
フロントソースファイル
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 style ="text-align :center">
<form id="form1" runat="server">
<div>
<table cellpadding = "0" cellspacing = "0" border= "1">
<tr>
<td align = "center">
<asp:GridView ID="gvBS" runat="server" AutoGenerateColumns="False" OnRowCreated="gvBS_RowCreated" ShowHeader="False">
<Columns>
<asp:BoundField DataField="BS" HeaderText="BS" />
<asp:BoundField DataField="BEYOND01" HeaderText="BEYOND01" />
<asp:BoundField DataField="BEYOND02" HeaderText="BEYOND02" />
<asp:BoundField DataField="BEYOND03" HeaderText="BEYOND03" />
<asp:BoundField DataField="BEYOND04" HeaderText="BEYOND04" />
<asp:BoundField DataField="YuQuan01" HeaderText="YuQuan01" />
<asp:BoundField DataField="YuQuan02" HeaderText="YuQuan02" />
<asp:BoundField DataField="Dream" HeaderText="Dream" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>