gridview-show-headers-only ソースコード全文

GridView_ShowHeaderWhenEmpty_CS.sln

GridView_ShowHeaderWhenEmpty_CS.sln
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.421
MinimumVisualStudioVersion = 10.0.40219.1
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "GridView_ShowHeaderWhenEmpty_CS", "GridView_ShowHeaderWhenEmpty_CS\", "{0B8D2951-A8AD-4AA1-B6C5-CAE31A755384}"
ProjectSection(WebsiteProperties) = preProject
TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.8"
Debug.AspNetCompiler.VirtualPath = "/localhost_8197"
Debug.AspNetCompiler.PhysicalPath = "GridView_ShowHeaderWhenEmpty_CS\"
Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_8197\"
Debug.AspNetCompiler.Updateable = "true"
Debug.AspNetCompiler.ForceOverwrite = "true"
Debug.AspNetCompiler.FixedNames = "false"
Debug.AspNetCompiler.Debug = "True"
Release.AspNetCompiler.VirtualPath = "/localhost_8197"
Release.AspNetCompiler.PhysicalPath = "GridView_ShowHeaderWhenEmpty_CS\"
Release.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_8197\"
Release.AspNetCompiler.Updateable = "true"
Release.AspNetCompiler.ForceOverwrite = "true"
Release.AspNetCompiler.FixedNames = "false"
Release.AspNetCompiler.Debug = "False"
VWDPort = "8197"
SlnRelativePath = "GridView_ShowHeaderWhenEmpty_CS\"
DefaultWebSiteLanguage = "Visual C#"
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0B8D2951-A8AD-4AA1-B6C5-CAE31A755384}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B8D2951-A8AD-4AA1-B6C5-CAE31A755384}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DCB38AE8-B68E-475D-9796-ACE25EA8A64A}
EndGlobalSection
EndGlobal

GridView_ShowHeaderWhenEmpty_CS/Default.aspx

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%-- ShowHeaderWhenEmpty="true" により、空データでもヘッダを表示する。 --%>
<asp:GridView
ID="GridView1"
runat="server"
ShowHeaderWhenEmpty="true"
>
</asp:GridView>
</div>
</form>
</body>
</html>

GridView_ShowHeaderWhenEmpty_CS/Default.aspx.cs

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["data"] == null)
{
DataTable dt = new DataTable();
// 列の定義
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
// 行データは設定しない
//dt.Rows.Add("", "", "");
Session["data"] = dt;
}
// 列定義だけを持つ DataTable を参照
this.GridView1.DataSource = Session["data"];
this.GridView1.DataBind();
}
}

GridView_ShowHeaderWhenEmpty_CS/Web.config

Web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
ASP.NET アプリケーションの構成方法の詳細については、
https://go.microsoft.com/fwlink/?LinkId=169433 を参照してください
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.8"/>
<httpRuntime targetFramework="4.8"/>
</system.web>
</configuration>

GridView_ShowHeaderWhenEmpty_CS/Web.Debug.config

Web.Debug.config
<?xml version="1.0" encoding="utf-8"?>
<!-- web.config 変換の使用方法の詳細については、https://go.microsoft.com/fwlink/?LinkId=125889 を参照してください -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
次の例では、"MyDB" という値を持つ "name" 属性が "Match" ロケーターで
見つかった場合にのみ、"SetAttributes" 変換によって "connectionString" の
値が "ReleaseSQLServer" を使用するように変更されます。
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
次の例では、web.config ファイルの <customErrors> セクション全体が
"Replace" 変換によって置き換えられます。
<system.web> ノードには customErrors セクションが 1 つしかないため、
"xdt:Locator" 属性を使用する必要はありません。
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
>
-->
</system.web>
</configuration>