【发布时间】:2010-10-22 00:55:37
【问题描述】:
当gridview中没有数据时如何显示页脚用于从页脚插入数据。
【问题讨论】:
标签: asp.net .net gridview visibility footer
当gridview中没有数据时如何显示页脚用于从页脚插入数据。
【问题讨论】:
标签: asp.net .net gridview visibility footer
最简单的方法是绑定一个长度为 1 的数组。您可以在其中放入任何您想确定这是虚拟行的内容。在您的 GridViews RowDataBound 方法上检查数据项是否是虚拟行(在尝试检查数据之前,首先确保 RowType 是 DataRow)。如果它是虚拟行,则将行可见性设置为 false。页脚和页眉现在应该显示没有任何数据。
确保在 GridView 上将 ShowFooter 属性设置为 true。
例如。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
myGrid.DataSource = new object[] {null};
myGrid.DataBind();
}
}
protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem == null)
{
e.Row.Visible = false;
}
}
}
【讨论】:
Here is the simple way GridView 有空数据时显示页脚。
【讨论】:
这是我制作的一些简单的东西:
/// <summary>
/// Ensures that the grid view will contain a footer even if no data exists.
/// </summary>
/// <typeparam name="T">Where t is equal to the type of data in the gridview.</typeparam>
/// <param name="gridView">The grid view who's footer must persist.</param>
public static void EnsureGridViewFooter<T>(GridView gridView) where T: new()
{
if (gridView == null)
throw new ArgumentNullException("gridView");
if (gridView.DataSource != null && gridView.DataSource is IEnumerable<T> && (gridView.DataSource as IEnumerable<T>).Count() > 0)
return;
// If nothing has been assigned to the grid or it generated no rows we are going to add an empty one.
var emptySource = new List<T>();
var blankItem = new T();
emptySource.Add(blankItem);
gridView.DataSource = emptySource;
// On databinding make sure the empty row is set to invisible so it hides it from display.
gridView.RowDataBound += delegate(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem == (object)blankItem)
e.Row.Visible = false;
};
}
要调用它,您可以使用以下命令:
MyGridView.DataSource = data;
EnsureGridViewFooter<MyDataType>(MyGridView);
MyGridView.DataBind();
希望这会有所帮助。干杯!
【讨论】: