簡體   English   中英

在空網格上顯示gridview頁腳?

[英]Show gridview footer on empty grid?

只是想知道即使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

    <DefaultValue(False), Category("Appearance"), Description("Include the footer when the table is empty")> _
    Property ShowFooterWhenEmpty As Boolean

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
    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<MyCustomClass> l = new List<MyCustomClass>();
            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子句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM