无记录时显示gridview表头,并增加一行显示“没有记录”绑定SqlDataSource控件时
Posted 杰的记事本——javascript.shop
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无记录时显示gridview表头,并增加一行显示“没有记录”绑定SqlDataSource控件时相关的知识,希望对你有一定的参考价值。
原文发布时间为:2008-08-04 —— 来源于本人的百度文章 [由搬家工具导入]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (GridView1.Rows.Count == 0)
creatHeader();
}
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
GridView1.PageIndex = GridView1.PageCount - 1;
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
}
private void creatHeader()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("class");
dt.Rows.Add(dt.NewRow());
GridView1.DataSourceID = "";//如果之前绑定的是SqlDataSource控件,这句必须写上
GridView1.DataSource = dt;
GridView1.DataBind();
int columnCount = dt.Columns.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan=columnCount;
GridView1.Rows[0].Cells[0].Text="没有记录";
GridView1.Rows[0].Cells[0].Style.Add("text-align","center");
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
if (GridView1.Rows.Count ==1)
creatHeader();
}
}
以上是关于无记录时显示gridview表头,并增加一行显示“没有记录”绑定SqlDataSource控件时的主要内容,如果未能解决你的问题,请参考以下文章
使用 grid.Mvc6 创建 gridview 时显示错误