Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否在空网格上显示gridview页脚?_C#_Asp.net_Gridview - Fatal编程技术网

C# 是否在空网格上显示gridview页脚?

C# 是否在空网格上显示gridview页脚?,c#,asp.net,gridview,C#,Asp.net,Gridview,只是想知道在gridview为空的情况下为数据输入显示gridview页脚的最佳和最简单的方法是什么?将数据源设置为绑定到gridview的对象类型,其中一个对象填充了空值,然后隐藏该数据行 编辑:因为您正在使用数据表 DataTable dt = new DataTable(); // Define all of the columns you are binding in your GridView dt.Columns.Add("AColumnName"); ... ... DataR

只是想知道在gridview为空的情况下为数据输入显示gridview页脚的最佳和最简单的方法是什么?

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

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

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
导入System.Web.UI.WebControls
导入System.ComponentModel
命名空间UI.WebControls
公共类GridViewExtended
继承GridView
Private\u footerRow作为GridViewRow
_
属性ShowFooterWhenEmpty为布尔值
_
Public将只读属性FooterRow替换为GridViewRow
得到
如果(我什么都不是)那么
Me.EnsureChildControl()
如果结束
还我
结束
端属性
受保护的重写函数CreateChildControls(ByVal数据源为System.Collections.IEnumerable,ByVal数据绑定为Boolean)为整数
Dim returnVal As Integer=MyBase.CreateChildControls(数据源,数据绑定)
如果returnVal=0,并且在何时显示Me.showFooter,则
Dim table As table=Me.Controls.Of type(Of table)()。First
Me.\u footerRow=Me.CreateRow(-1,-1,DataControlRowType.Footer,DataControlRowState.Normal,dataBinding,Nothing,Me.Columns.Cast(属于DataControlField)。ToArray,table.Rows,Nothing)
如果不是我,那么ShowFooter
_footerRow.Visible=False
如果结束
如果结束
返回值
端函数
私有重载函数CreateRow(ByVal rowIndex为整数,ByVal dataSourceIndex为整数,ByVal rowType为DataControlRowType,ByVal rowState为DataControlRowState,ByVal dataBind为布尔值,ByVal dataItem为对象,ByVal字段为DataControlField(),ByVal行为TableRowCollection,ByVal pagedDataSource为pagedDataSource为pagedDataSource)作为GridViewRow
按GridViewRow=Me.CreateRow(行索引、数据源索引、行类型、行状态)对行进行调整
将e变暗为新的GridViewRowEventArgs(行)
如果(rowType DataControlRowType.Pager),则
Me.InitializeRow(行,字段)
其他的
Me.InitializePage(行、字段、长度、页面数据源)
如果结束
如果是数据绑定,那么
row.DataItem=DataItem
如果结束
Me.OnRowCreated(e)
行。添加(行)
如果是数据绑定,那么
row.DataBind()
Me.OnRowDataBound(e)
row.DataItem=无
如果结束
返回行
端函数
末级
结束命名空间

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

更准确地说,在查询的SELECT子句末尾添加列“0 AS dummyRow”,然后将所有完整语句合并到

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
 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

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

作为旁注,如果您希望有条件地显示网格的页眉和页脚或显示空数据文本/模板,在使用上面发布的代码隐藏行后,您可以检查您的条件,并在必要时删除该行。T然后代码将如下所示:

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
 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行(这是必要的,因为在回发时gridview可能会在不重新数据绑定的情况下重新绘制自身)和gridview行本身(这是必要的,因为此时该行已经在网格的子表中了,这是我们不希望看到的)


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

理想情况下,您只希望在表中没有记录时显示虚拟行。因此,请将SelectCommand设置为类似以下内容:

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
 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
选择[ID]、姓氏、姓氏、来自客户的电子邮件 联盟 选择0[ID]、“”FirstName、“”LastName、“”Email其中0(从客户处选择计数(1)

这样,如果计数>0,则不会返回虚拟行

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

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

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;
        }
if(list!=null&&list.Any())
{
gridView.DataSource=list;
gridView.DataBind();
}
其他的
{
MyCustomClass项=新的MyCustomClass(){Id=0,Name=“(无数据行)”,Active=false};
列表l=新列表();
l、 增加(项目);
gridView.DataSource=l;
gridView.DataBind();
gridView.Rows[0]。Visible=false;
}

我使用的是datatable,你有什么样的示例吗?我觉得这有点笨重,但我的时间不够,所以这个修复对我来说很有效。我必须通过添加myGridView.Rows[0]。visible=false.C#version来隐藏gridview中的“空”行