首页 > 代码库 > 无记录时显示gridview表头,并增加一行显示“没有记录”【绑定SqlDataSource控件时】
无记录时显示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控件时】