使用说明:直接将下面代码直接创建一个类,就可以调用。
用途:
Gridview绑定的数据记录为空时显示Gridview的表头,并显示没有记录的提示
Imports System.Data
Imports System.Web.UI.WebControls
'''<summary>
''' Gridview绑定的数据记录为空时显示Gridview的表头,并显示没有记录的提示
'''</summary>
Public Class GridviewControl
'当Gridview数据为空时显示的信息
Private Shared EmptyText As String = "没有记录"
Public Sub New()
End Sub
'''<summary>
'''防止PostBack后Gridview不能显示
'''</summary>
'''<param name="gridview"></param>
Public Shared Sub ResetGridView(ByVal gridview As GridView)
'如果数据为空则重新构造Gridview
If gridview.Rows.Count = 1 AndAlso gridview.Rows(0).Cells(0).Text = EmptyText Then
Dim columnCount As Integer = gridview.Columns.Count
gridview.Rows(0).Cells.Clear()
gridview.Rows(0).Cells.Add(New TableCell())
gridview.Rows(0).Cells(0).ColumnSpan = columnCount
gridview.Rows(0).Cells(0).Text = EmptyText
gridview.Rows(0).Cells(0).Style.Add("text-align", "center")
End If
End Sub
'''<summary>
'''绑定数据到GridView,当表格数据为空时显示表头
'''</summary>
'''<param name="gridview"></param>
'''<param name="table"></param>
Public Shared Sub GridViewDataBind(ByVal gridview As GridView, ByVal table As DataTable)
'记录为空重新构造Gridview
If table.Rows.Count = 0 Then
table = table.Clone()
table.Rows.Add(table.NewRow())
gridview.DataSource = table
gridview.DataBind()
Dim columnCount As Integer = table.Columns.Count
gridview.Rows(0).Cells.Clear()
gridview.Rows(0).Cells.Add(New TableCell())
gridview.Rows(0).Cells(0).ColumnSpan = columnCount
gridview.Rows(0).Cells(0).Text = EmptyText
gridview.Rows(0).Cells(0).Style.Add("text-align", "center")
Else
'数据不为空直接绑定
gridview.DataSource = table
gridview.DataBind()
End If
'重新绑定取消选择
gridview.SelectedIndex = -1
End Sub
End Classgridview不显示
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论