让Gridview在没有数据的时候显示

本文详细介绍了在ASP.NET网站中使用GridView控件进行数据绑定时,如何处理空数据的问题,包括显示提示信息、增加空行显示表头等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当对 GridView 控件进行数据绑定时,如果绑定的记录为空,网页上就不显示 GridView,造成页面部分空白,页面布局结构也受影响。下面讨论的方法可以让 GridView 在没有数据记录的时候显示表的字段结构和显示提示信息。


为了让 GridView 显示数据,在数据库中建立表 Student,其字段如下:
id  nchar(10)
name  nchar(10)
location  nchar(10)
date  nchar(10)


建立一个 asp.net 网站工程,在页面中添加 GridView,代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Teat02.WebForm2" %>

<!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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="学生id">
                    <ItemTemplate>
                        <%#Eval("id")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="姓名">
                    <ItemTemplate>
                        <%#Eval("name")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="住址">
                    <ItemTemplate>
                        <%#Eval("location")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="入学时间">
                    <ItemTemplate>
                        <%#Eval("date")%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

界面如图:



GridView 要绑定的字段和 Student 的字段一样,在这里我们利用 GridView 原有的功能,设定 EmptyDataText 属性,当数据为空是显示“ 数据为空 ”,如果没有设定 EmptyDataText 属性,当绑定的记录为空时,GridView 将不在页面显示。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Teat02
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private SqlDataReader sdr = null;
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            string connStr = "server=wlm-PC;database=Test01;uid=sa;pwd=123456";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand sqlc = new SqlCommand("select * from Student", conn);
            sdr = sqlc.ExecuteReader();
            dt.Load(sdr);
            getDataBind();
        }

        private void getDataBind()
        {
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }
    }
}

显示的结果:



增加空行来显示 GridView ,我们知道只要 GridView 绑定的 DataTable 有一行记录,GridView 就会显示表头,所以当 DataTable 为空时我们增加一个空行从而显示表头。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Teat02
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private SqlDataReader sdr = null;
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            string connStr = "server=wlm-PC;database=Test01;uid=sa;pwd=123456";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand sqlc = new SqlCommand("select * from Student", conn);
            sdr = sqlc.ExecuteReader();
            dt.Load(sdr);
            getDataBind();

        }

        private void getDataBind()
        {
            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
            }
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
            int colnumcount = dt.Columns.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = colnumcount;
            GridView1.Rows[0].Cells[0].Text = "没有相关记录";
            GridView1.Rows[0].Cells[0].Style.Add("color", "red");
        }
    }
}

显示结果:



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值