当前位置:  开发笔记 > 编程语言 > 正文

在空网格上显示gridview页脚?

如何解决《在空网格上显示gridview页脚?》经验,为你挑选了2个好方法。

只是想知道即使gridview为空,显示gridview页脚进行数据输入的最佳和最简单的方法是什么?



1> TheGeekYouNe..:

将数据源设置为您绑定到GridView的对象类型,其中一个对象填充空值,然后隐藏该DataRow.

编辑:因为你正在使用数据表......

DataTable dt = new DataTable();

// Define all of the columns you are binding in your GridView
dt.Columns.Add("AColumnName");
...
...

DataRow dr = dt.NewRow();
dt.Rows.Add(dr);

myGridView.DataSource = dt;
myGridView.DataBind();


我觉得这有点笨重,但我的时间不足,所以这个修复对我有用.我不得不通过添加myGridView.Rows [0] .visible = false来隐藏gridview中的那行"空"行.

2> Richard Coll..:

更优雅..扩展GridView并添加ShowFooterWhenEmpty属性,这样您就不必在任何地方实现自定义代码.

Imports System.Web.UI.WebControls
Imports System.ComponentModel

Namespace UI.WebControls
Public Class GridViewExtended
    Inherits GridView

    Private _footerRow As GridViewRow

     _
    Property ShowFooterWhenEmpty As Boolean

     _
    Public Overrides ReadOnly Property FooterRow As GridViewRow
        Get
            If (Me._footerRow Is Nothing) Then
                Me.EnsureChildControls()
            End If
            Return Me._footerRow
        End Get
    End Property

    Protected Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer
        Dim returnVal As Integer = MyBase.CreateChildControls(dataSource, dataBinding)
        If returnVal = 0 AndAlso Me.ShowFooterWhenEmpty Then
            Dim table As Table = Me.Controls.OfType(Of Table)().First
            Me._footerRow = Me.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal, dataBinding, Nothing, Me.Columns.Cast(Of DataControlField).ToArray, table.Rows, Nothing)
            If Not Me.ShowFooter Then
                _footerRow.Visible = False
            End If
        End If
        Return returnVal
    End Function

    Private Overloads Function CreateRow(ByVal rowIndex As Integer, ByVal dataSourceIndex As Integer, ByVal rowType As DataControlRowType, ByVal rowState As DataControlRowState, ByVal dataBind As Boolean, ByVal dataItem As Object, ByVal fields As DataControlField(), ByVal rows As TableRowCollection, ByVal pagedDataSource As PagedDataSource) As GridViewRow
        Dim row As GridViewRow = Me.CreateRow(rowIndex, dataSourceIndex, rowType, rowState)
        Dim e As New GridViewRowEventArgs(row)
        If (rowType <> DataControlRowType.Pager) Then
            Me.InitializeRow(row, fields)
        Else
            Me.InitializePager(row, fields.Length, pagedDataSource)
        End If
        If dataBind Then
            row.DataItem = dataItem
        End If
        Me.OnRowCreated(e)
        rows.Add(row)
        If dataBind Then
            row.DataBind()
            Me.OnRowDataBound(e)
            row.DataItem = Nothing
        End If
        Return row
    End Function

End Class
End Namespace

推荐阅读
LEEstarmmmmm
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有