GridView绑定空表提示
当查询结果为空时,不弹出提示,而是直接绑定空表,空表下第一列是说明,代码:
//如果没有查询到结果,则绑定一个空表,并给出提示
if (ds.Tables[0].Rows.Count == 0)
{
//先给空表添加一新行
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
//给gridview绑定空表
this.GV_MCText.DataSource = ds;
this.GV_MCText.DataBind();
//获取表行的列数
int CellCount = this.GV_MCText.Rows[0].Cells.Count;
//清除一下第一行所有列
this.GV_MCText.Rows[0].Cells.Clear();
//在第一行添加一个新列
this.GV_MCText.Rows[0].Cells.Add(new TableCell());
//该列覆盖全行
this.GV_MCText.Rows[0].Cells[0].ColumnSpan = CellCount;
//最后显示提示字符
this.GV_MCText.Rows[0].Cells[0].Text = "没有数据";
}
扩展一下,可以写成类文件,供所有模块调用
#region 用户查询条件绑定GridView
public static void BindSearchReturn(GridView gv, string itcode,int search)
{
string MySql = "select SaveSearch,Names,CreateDate from GTN_SaveSearch where itcode= '" + itcode + "' and State='0' and SearchType="+search;
DataTable dt = LenovoFrame.Library.Data.Search.UserPersonSearch.BindSearchParam(MySql);
if (dt.Rows.Count == 0)
{
dt.Rows.Add(dt.NewRow());
gv.DataSource = dt;
gv.DataBind();
int CellCount = gv.Rows[0].Cells.Count;
gv.Rows[0].Cells.Clear();
gv.Rows[0].Cells.Add(new TableCell());
gv.Rows[0].Cells[0].ColumnSpan = CellCount;
gv.Rows[0].Cells[0].Text = "No Records Found";
}
else
{
gv.DataSource = dt;
gv.DataBind();
}
}
#endregion