只是想知道即使gridview为空,显示gridview页脚进行数据输入的最佳和最简单方法是什么?
答案 0 :(得分:15)
将数据源设置为您绑定到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();
答案 1 :(得分:9)
更优雅..扩展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
答案 2 :(得分:3)
另一个解决方案是始终在数据源中添加一个虚拟行,“标记”具有特定值的行,然后在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项目部署自定义控件库。
答案 3 :(得分:1)
作为旁注,如果您想有条件地显示网格的页眉和页脚或显示空数据文本/模板,在您使用我上面发布的代码隐藏行之后,您可以检查您的状况并在必要时删除该行。然后代码看起来像这样:
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有更多行时,您可以使用类似的代码删除虚拟行。
答案 4 :(得分:1)
你可以创建一个&#34;空&#34;行并使其不可见:
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;
}
答案 5 :(得分:0)
理想情况下,如果表中没有记录,您只想显示虚拟行。因此,将SelectCommand设置为以下内容:
SELECT [ID],FirstName,LastName,来自客户的电子邮件 联盟 选择0 [ID],&#39;&#39; FirstName,&#39;&#39;姓氏,&#39;&#39;电子邮件其中0(来自客户的SELECT COUNT(1))
如果计数&gt; 0,虚拟行没有返回。
请注意,虚拟行中没有FROM子句。