方法一:通过Row.add()增加一新行来实现
数据源可以是DateSet或者SqlDataSource或者别的
public void NoRecordBuild(GridView gridView, DataSet ds)
...{
if (ds.Tables[0].Rows.Count == 0)
...{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gridView.DataSource = ds;
gridView.DataBind();
int columnCount = gridView.Rows[0].Cells.Count;
gridView.Rows[0].Cells.Clear();
gridView.Rows[0].Cells.Add(new TableCell());
gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
gridView.Rows[0].Cells[0].Text = "No Records Found.";
}
else
...{
gridView.DataSource = ds;
gridView.DataBind();
}
}
...{
if (ds.Tables[0].Rows.Count == 0)
...{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gridView.DataSource = ds;
gridView.DataBind();
int columnCount = gridView.Rows[0].Cells.Count;
gridView.Rows[0].Cells.Clear();
gridView.Rows[0].Cells.Add(new TableCell());
gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
gridView.Rows[0].Cells[0].Text = "No Records Found.";
}
else
...{
gridView.DataSource = ds;
gridView.DataBind();
}
}
方法二:通过在编辑EmptyDataTemplate(空模板)实现
<EmptyDataRowStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EmptyDataTemplate>
<table>
<tr>
<td style="width: 100px">
列名1</td>
<td style="width: 100px">
列名2</td>
<td style="width: 100px">
列名3</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyDataTemplate>
<table>
<tr>
<td style="width: 100px">
列名1</td>
<td style="width: 100px">
列名2</td>
<td style="width: 100px">
列名3</td>
</tr>
</table>
</EmptyDataTemplate>
注意 要让他的样式和headrow一样
方法三:没有数据换一个有一行空数据的数据源
后台:数据源是sqldatasource
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;
using System.Data.SqlClient;
public partial class gridview没数据显示表头_ : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
GridView1.DataSource = SqlDataSource1;//最好是在代码里绑定,这样认识的更清晰
SqlDataSource1.SelectCommand = "SELECT [ProductID],[ProductName],[SupplierID] FROM [Alphabetical list of products]";
GridView1.DataBind();
}
//没有数据换一个有一行空数据的数据源
public void NoRecordBuild(GridView gridView, SqlDataSource sqlds)
...{
SqlConnection connect = new SqlConnection(sqlds.ConnectionString);
connect.Open();
DataTable dt = new DataTable();
System.Data.SqlClient.SqlDataAdapter adpter = new System.Data.SqlClient.SqlDataAdapter(sqlds.SelectCommand, connect);
adpter.Fill(dt);
DataRow dr = dt.NewRow();
dt.Clear();
dt.Rows.Add(dr);
GridView1.DataSourceID = "";//绑定新数据源前先将原来的清理
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
...{
}
protected void Button1_Click(object sender, EventArgs e)
...{
SqlDataSource1.SelectCommand = "SELECT [ProductID],[ProductName],[SupplierID] FROM [Alphabetical list of products] WHERE ([CategoryID] = '0')";
GridView1.DataBind();
if (GridView1.Rows.Count == 0)
...{
NoRecordBuild(GridView1, SqlDataSource1);
}
}
}
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;
using System.Data.SqlClient;
public partial class gridview没数据显示表头_ : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
GridView1.DataSource = SqlDataSource1;//最好是在代码里绑定,这样认识的更清晰
SqlDataSource1.SelectCommand = "SELECT [ProductID],[ProductName],[SupplierID] FROM [Alphabetical list of products]";
GridView1.DataBind();
}
//没有数据换一个有一行空数据的数据源
public void NoRecordBuild(GridView gridView, SqlDataSource sqlds)
...{
SqlConnection connect = new SqlConnection(sqlds.ConnectionString);
connect.Open();
DataTable dt = new DataTable();
System.Data.SqlClient.SqlDataAdapter adpter = new System.Data.SqlClient.SqlDataAdapter(sqlds.SelectCommand, connect);
adpter.Fill(dt);
DataRow dr = dt.NewRow();
dt.Clear();
dt.Rows.Add(dr);
GridView1.DataSourceID = "";//绑定新数据源前先将原来的清理
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
...{
}
protected void Button1_Click(object sender, EventArgs e)
...{
SqlDataSource1.SelectCommand = "SELECT [ProductID],[ProductName],[SupplierID] FROM [Alphabetical list of products] WHERE ([CategoryID] = '0')";
GridView1.DataBind();
if (GridView1.Rows.Count == 0)
...{
NoRecordBuild(GridView1, SqlDataSource1);
}
}
}
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview没数据显示表头 .aspx.cs" Inherits="gridview没数据显示表头_" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>