在空网格上显示gridview页脚?

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

将数据源设置为您绑定到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(); 

更优雅..扩展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 

另一种解决方案是始终在数据源中添加一个虚拟行,“标记”具有特定值的行,然后隐藏RowDataBound上的行。

更确切地说,将“0 AS dummyRow”列添加到查询的SELECT子句的末尾,然后将UNION ALL的完整语句添加到

 SELECT NULL AS column1, NULL AS column2,...,NULL AS columnN, 1 AS dummyRow 

一旦你有了查询(所有这些都可以在你的SQLDataSource或你的DAL对象中完成,你的网格代码将如下所示:

 Protected Sub MyGridView_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowDataBound If (e.Row.RowType = DataControlRowType.DataRow) AndAlso (Not e.Row.DataItem Is Nothing) AndAlso (CInt(e.Row.DataItem("dummyRow")) = 1) Then e.Row.Visible = False End If End Sub 

这个解决方案带来了一些明显的开销,因为这个检查将针对结果的每一行进行,更不用说你必须改变你的SELECT查询,但它还具有不需要动态更改数据集的优点(如第一个例子)并且不需要太多代码或不必为您的Web项目部署自定义控件库。

作为旁注,如果你想有条件地显示网格的页眉和页脚或显示空数据文本/模板,在你用上面发布的代码隐藏行之后,你可以检查你的情况,如果有必要删除行。 然后代码看起来像这样:

  Protected Sub MyGridView_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowDataBound If (e.Row.RowType = DataControlRowType.DataRow) AndAlso (Not e.Row.DataItem Is Nothing) AndAlso (CInt(e.Row.DataItem("dummyRow")) = 1) Then e.Row.Visible = False If (ConditionToShowEmptyDataTemplate) Then CType(e.Row.DataItem, System.Data.DataRowView).Delete() CType(e.Row.Parent, System.Web.UI.WebControls.Table).Rows.Remove(e.Row) End If End Sub 

请注意,这里我们删除了DataItem行(必要的因为在post-backs上gridview可能重绘自己而没有重新数据绑定)和GridView Row本身(必要因为此时行已经在网格的Childtable中了,我们不知道我想)。

最后,如果隐藏的虚拟记录在其有其他数据(例如,错误的分页)时导致gridview中的其他问题,则当gridview有更多行时,您可以使用类似的代码来删除虚拟行。

您可以创建一个“空”行并使其不可见:

 if (list != null && list.Any()) { gridView.DataSource = list; gridView.DataBind(); } else { MyCustomClass item = new MyCustomClass(){Id = 0, Name = "(No Data Rows)", Active = false}; List l = new List(); l.Add(item); gridView.DataSource = l; gridView.DataBind(); gridView.Rows[0].Visible = false; } 

理想情况下,如果表中没有记录,您只想显示虚拟行。 所以将SelectCommand设置为这样的:

SELECT [ID],FirstName,LastName,Email FROM Customers union 0 [ID],”FirstName,”LastName,”电子邮件,其中0 in(SELECT COUNT(1)from Customers)

这样,如果count> 0,则不返回虚拟行。

请注意,虚拟行中没有FROM子句。