原文源地址:扩展 GridView 控件 - 无数据时显示标题列 :VB.net Version
此文章,我一看觉得在平时还是比较有用的。所以做一下C# Convert VB.net
(仅针对于新手,需要者浏览)
Page Code
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<gvExtrend:GridViewEmptyHeader ID="gvEmptyHeader" runat="server" AutoGenerateColumns="False"
EmptyShowHeader="True" EmptyDataText="Empty Data" AllowPaging="True"
DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
SortExpression="SupplierID" />
</Columns>
</gvExtrend:GridViewEmptyHeader>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [ProductID], [ProductName], [SupplierID] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:Parameter DefaultValue="99999" Name="CategoryID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Web.Config 添加注册Tag
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<pages>
<controls>
<add tagPrefix="gvExtrend" namespace="GridViewExtrend" assembly="GridViewExtrend"/>
</controls>
</pages>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GridViewExtrend Source
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace GridViewExtrend
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
using System.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Drawing;
using System.Collections;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Description("GridViewEmptyHeader"), ToolboxData("<{0}:GvEmptyHeader runat=server><{0}:GvEmptyHeader>")]
public class GridViewEmptyHeader : GridView
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Boolean fEmptyShowHeader = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 无数据时是否显示字段标题
/// </summary>
public Boolean EmptyShowHeader
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return fEmptyShowHeader;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fEmptyShowHeader = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 建立子控件
/// </summary>
/// <param name="dataSource"></param>
/// <param name="dataBinding"></param>
/// <returns>建立的数据列数目</returns>
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Int32 rowCount;
Table table = new Table();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
rowCount = base.CreateChildControls(dataSource, dataBinding);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this.fEmptyShowHeader && (rowCount == 0))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
table = CreateEmptyTable();
Controls.Clear();
Controls.Add(table);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return rowCount;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Table CreateEmptyTable()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Table table = new Table();
GridViewRow gridViewRow;
TableCell cell = new TableCell();
Int32 count;
GridViewRowEventArgs e;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
table = base.CreateChildTable();
count = this.Columns.Count - 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Create Title Columns
gridViewRow = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
DataControlField[] fields = new DataControlField[count + 1];
this.Columns.CopyTo(fields, 0);
this.InitializeRow(gridViewRow, fields);
e = new GridViewRowEventArgs(gridViewRow);
this.OnRowCreated(e);
table.Rows.Add(gridViewRow);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Create Empty DataColumns
gridViewRow = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
cell.ColumnSpan = fields.Length;
cell.Width = Unit.Percentage(100);
cell.Text = this.EmptyDataText;
cell.HorizontalAlign = HorizontalAlign.Center;
gridViewRow.Cells.Add(cell);
table.Rows.Add(gridViewRow);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return table;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)