GridView控件无数据时显示字段列_[Asp.Net教程]
当 GridView 系结的 DataSource 资料笔数为 0 时,会依 EmptyDataTemplate 及 EmptyDataText 的设定来显示无数据的状态。若我们希望 GridView 在无数据时,可以显示字段标题,有一种作法是在 EmptyDataTemplate 中手动在设定一个标题列,不过这种作法很麻烦。GridView 控件可不可以直接透过属性设定就可以在无数据显示字段标题呢?答案是肯定的,本文将扩展 GridView 控件来达成此需求。
扩展 GridView 控件
我们继承 GridView 命名为 TBGridView,新增一个 EmptyShowHeader 属性,来设定无数据时是否显示字段标题。覆写 CreateChildControls 方法,当 Mybase.CreateChildControls 传回 0 时,表示 DataSource 无数据,此时就呼叫 CreateEmptyTable 方法来建立无数据只显示标题的表格。
在 CreateEmptyTable 方法中,会复制 Columns 的集合,来输出所有字段的标题列。接下来会在标题列下方新增一列合并的数据列,用来显示无数据时的显示文字,即显示 EmptyDataText 属性值。
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing
Namespace WebControlsNamespace WebControls
< _
Description("TBGridView 控件"), _
ToolboxData("<{0}:TBGridView runat=server>{0}:TBGridView>") _
> _
Public Class TBGridViewClass TBGridView
Inherits GridView
Private FEmptyShowHeader As Boolean = True
/**/’’’
’’’ 无数据时是否显示字段标题。
’’’ Public Property EmptyShowHeader()Property EmptyShowHeader() As Boolean
Get
Return FEmptyShowHeader
End Get
Set(ByVal value As Boolean)
FEmptyShowHeader = value
End Set
End Property
/**/’’’
’’’ 建立子控件。
’’’ ’’’
控件的数据来源。
’’’
true 指示子控件系结至数据,否则为 false。
’’’
建立的数据列数目。 Protected Overrides Function CreateChildControls()Function CreateChildControls(ByVal DataSource As System.Collections.IEnumerable, ByVal DataBinding As Boolean) As Integer
Dim iRowCount As Integer
Dim oTable As Table
iRowCount = MyBase.CreateChildControls(DataSource, DataBinding)
If Me.EmptyShowHeader AndAlso (iRowCount = 0) Then
oTable = CreateEmptyTable()
Me.Controls.Clear()
Me.Controls.Add(oTable)
End If
End Function
/**/’’’
’’’ 建立无数据只显示标题的表格。
’’’ Private Function CreateEmptyTable()Function CreateEmptyTable() As Table
Dim oTable As Table
Dim oGridViewRow As GridViewRow
Dim oCell As TableCell
Dim iCount As Integer
Dim e As GridViewRowEventArgs
oTable = MyBase.CreateChildTable()
iCount = Me.Columns.Count - 1
’建立标题列
oGridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
Dim oFields(iCount) As DataControlField
Me.Columns.CopyTo(oFields, 0) ’取得目前定义 Columns 复本
Me.InitializeRow(oGridViewRow, oFields) ’资料列初始化
e = New GridViewRowEventArgs(oGridViewRow)
Me.OnRowCreated(e) ’引发 RowCreated 事件
oTable.Rows.Add(oGridViewRow)
’建立空白的数据列
oGridViewRow = New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
oCell = New TableCell()
oCell.ColumnSpan = oFields.Length
oCell.Width = Unit.Percentage(100)
oCell.Text = Me.EmptyDataText
oCell.HorizontalAlign = UI.WebControls.HorizontalAlign.Center
oGridViewRow.Cells.Add(oCell)
oTable.Rows.Add(oGridViewRow)
Return oTable
End Function
End Class
End Namespace
测试
我们拖曳一个 TBGridView 控件至页面上,设定 EmptyShowHeader="True"。
12 EmptyShowHeaer="True" EmptyDataText="沒有資料錄可顯示。" AllowPaging="True" DataKeyNames="EmployeeID" CellPadding="4" ForeColor="#333333" GridLines="None">
执行程序,当 GridView 有数据时的画面如下

而当 GridView 无数据时,就会显示字段列及无数据的显示文字(EmptyDataText)。

来源:http://www.cnblogs.com/jeff377