原文源地址:扩展 GridView 控件 - 无数据时显示标题列 :VB.net Version
此文章,我一看觉得在平时还是比较有用的。所以做一下C# Convert VB.net
(仅针对于新手,需要者浏览)
Page Code
< 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 >
Web.Config 添加注册Tag
< pages >
< controls >
< add tagPrefix = " gvExtrend " namespace = " GridViewExtrend " assembly = " GridViewExtrend " />
</ controls >
</ pages >
GridViewExtrend Source
namespace GridViewExtrend
{
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;
[Description("GridViewEmptyHeader"), ToolboxData("<{0}:GvEmptyHeader runat=server><{0}:GvEmptyHeader>")]
public class GridViewEmptyHeader : GridView
{
private Boolean fEmptyShowHeader = true;
/**//// <summary>
/// 无数据时是否显示字段标题
/// </summary>
public Boolean EmptyShowHeader
{
get
{
return fEmptyShowHeader;
}
set
{
fEmptyShowHeader = value;
}
}
/**//// <summary>
/// 建立子控件
/// </summary>
/// <param name="dataSource"></param>
/// <param name="dataBinding"></param>
/// <returns>建立的数据列数目</returns>
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
Int32 rowCount;
Table table = new Table();
rowCount = base.CreateChildControls(dataSource, dataBinding);
if (this.fEmptyShowHeader && (rowCount == 0))
{
table = CreateEmptyTable();
Controls.Clear();
Controls.Add(table);
}
return rowCount;
}
private Table CreateEmptyTable()
{
Table table = new Table();
GridViewRow gridViewRow;
TableCell cell = new TableCell();
Int32 count;
GridViewRowEventArgs e;
table = base.CreateChildTable();
count = this.Columns.Count - 1;
//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);
//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);
return table;
}
}
}
< 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 >
Web.Config 添加注册Tag
< pages >
< controls >
< add tagPrefix = " gvExtrend " namespace = " GridViewExtrend " assembly = " GridViewExtrend " />
</ controls >
</ pages >
GridViewExtrend Source
namespace GridViewExtrend
{
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;
[Description("GridViewEmptyHeader"), ToolboxData("<{0}:GvEmptyHeader runat=server><{0}:GvEmptyHeader>")]
public class GridViewEmptyHeader : GridView
{
private Boolean fEmptyShowHeader = true;
/**//// <summary>
/// 无数据时是否显示字段标题
/// </summary>
public Boolean EmptyShowHeader
{
get
{
return fEmptyShowHeader;
}
set
{
fEmptyShowHeader = value;
}
}
/**//// <summary>
/// 建立子控件
/// </summary>
/// <param name="dataSource"></param>
/// <param name="dataBinding"></param>
/// <returns>建立的数据列数目</returns>
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
Int32 rowCount;
Table table = new Table();
rowCount = base.CreateChildControls(dataSource, dataBinding);
if (this.fEmptyShowHeader && (rowCount == 0))
{
table = CreateEmptyTable();
Controls.Clear();
Controls.Add(table);
}
return rowCount;
}
private Table CreateEmptyTable()
{
Table table = new Table();
GridViewRow gridViewRow;
TableCell cell = new TableCell();
Int32 count;
GridViewRowEventArgs e;
table = base.CreateChildTable();
count = this.Columns.Count - 1;
//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);
//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);
return table;
}
}
}