数据源为空GridView显示表头和提示

来源:互联网 发布:linux系统的qq rpm包 编辑:程序博客网 时间:2024/05/18 02:36

 方法一:采用GridView中的EmptyTemplate来实现,模版中写一个静态的table;

下面贴出aspx代码:

<head runat="server">
    <title>GridView使用EmptyTemplate来使无数据时显示表头</title>
</head>
<body>
    <form id="form1" runat="server">
    <strong>GridView使用EmptyTemplate来使无数据时显示表头</strong>
    <div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1" BackColor="White" BorderColor="#336666" Width="600px"
            BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <EmptyDataTemplate>
                <table cellpadding="0" cellspacing="0" width="600px">
                    <tr>
                       <td width="100px">
                          用户编号
                       </td>
                       <td width="100px">
                          用户姓名
                       </td>
                       <td width="100px">
                          用户类型
                       </td>
                       <td width="100px">
                          用户年龄
                       </td>
                       <td width="100px">
                          电子邮件
                       </td>
                       <td width="100px">
                          联系地址
                       </td>
                    </tr>
                    <tr>
                       <td colspan="6" align="center">
                          没有数据显示
                       </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
        </asp:GridView>
   
    </div>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/GridViewTest.mdb"
            SelectCommand="SELECT [UuserID], [UuserName], [UuserPower], [Age], [Email], [Address] FROM [Uuser]">
        </asp:AccessDataSource>
    </form>
</body>
</html>

因为我用了AccessDataSource 数据源控件所以在cs文件中不用写什么代码了,但是如果没有用数据源控件的话,cs文件就应该要写上GridView的数据绑定代码了,就是那么简单.

方法二: 若数据源为DataTable,则当无数据时,始终返回一个空行的DataTable;
若数据源是集合类(ArrayList,List<T>等),无数据时,生成一个空的实体,加入到集合类中.
aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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>GridView使用空行在无数据时显示表头</title>
</head>
<body>
    <form id="form1" runat="server">
    <strong>GridView使用空行在无数据时显示表头</strong>
    <div>
        <asp:GridView ID="GridView1" runat="server" BackColor="White" Width="600px"
            BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
            GridLines="Horizontal">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

这里就是拖了一个GridView控件,主要是在cs文件里实现:下面是cs文件代码:

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.OleDb;
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("Provider=Microsoft.Jet.OLEDB.4.0; ");
        sb.Append("Data Source=");
        sb.Append(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/GridViewTest.mdb"));
        String sql = "select * from Uuser";
        OleDbConnection conn = new OleDbConnection(sb.ToString());
        conn.Open();
        OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
        DataTable table = new DataTable();
        da.Fill(table);
        if (table.Rows.Count > 0)
        {
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
        }
        else
        {
            DataRow dr = table.NewRow();
            table.Rows.Add(dr);
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
        }
        conn.Close();
    }
}

方法三:虽然比较麻烦,但是这个是比较有效同时 扩展GridView的功能.继承GridVie,重写Render方法,当其数据源为空时做一下处理,我把它做成DLL,下面是DLL中的代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace GridViewControl
{
    public class TGridView : System.Web.UI.WebControls.GridView
    {
        private Boolean _enableEmptyContentRender = true;
        private String _EmptyDataCellCssClass;
        /// <summary>
        /// 数据源为空时是否显示列头
        /// </summary>
        public Boolean EnableEmptyContentRender
        {
            get
            {
                return _enableEmptyContentRender;
            }
            set
            {
                _enableEmptyContentRender = value;
            }
        }

        /// <summary>
        /// 数据为空时信息单元格样式类
        /// </summary>
        public String EmptyDataCellCssClass
        {
            get
            {
                return _EmptyDataCellCssClass;
            }
            set
            {
                _EmptyDataCellCssClass = value;
            }
        }

        /// <summary>
        /// 为空时输出内容
        /// </summary>
        /// <param name="writer"></param>
        protected virtual void RenderEmptyContent(System.Web.UI.HtmlTextWriter writer)
        {
            System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table();
            table.CssClass = this._EmptyDataCellCssClass;
            table.GridLines = this.GridLines;
            table.CellPadding = this.CellPadding;
            table.CellSpacing = this.CellSpacing;
            table.BorderStyle = this.BorderStyle;
            table.BorderWidth = this.BorderWidth;
            table.HorizontalAlign = this.HorizontalAlign;
            table.Width = this.Width;
            table.CopyBaseAttributes(this);
            System.Web.UI.WebControls.TableRow tr = new System.Web.UI.WebControls.TableRow();
            table.Rows.Add(tr);
            foreach (System.Web.UI.WebControls.DataControlField field in this.Columns)
            {
                System.Web.UI.WebControls.TableCell cell = new System.Web.UI.WebControls.TableCell();
                cell.Text = field.HeaderText;
                cell.CssClass = this.CssClass;
                tr.Cells.Add(cell);
            }
            System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();
            table.Rows.Add(row);
            System.Web.UI.WebControls.TableCell msgCell = new System.Web.UI.WebControls.TableCell();
            msgCell.CssClass = this._EmptyDataCellCssClass;
            if (this.EmptyDataTemplate != null)
            {
                this.EmptyDataTemplate.InstantiateIn(msgCell);
            }
            else
            {
                msgCell.Text = this.EmptyDataText;
            }
            msgCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            msgCell.ColumnSpan = this.Columns.Count;
            row.Cells.Add(msgCell);
            table.RenderControl(writer);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (this._enableEmptyContentRender == true && (this.Rows.Count == 0 || this.Rows[0].RowType == System.Web.UI.WebControls.DataControlRowType.EmptyDataRow))
            {
                RenderEmptyContent(writer);
            }
            else
            {
                base.Render(writer);
            }
        }
    }
}
下面是aspx的代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="TGridView" Namespace="GridViewControl" Assembly="GridViewControl" %>
<!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>重写GridView使无数据时显示表头</title>
    <style type="text/css">
       .table
       {
           BackColor="#336666";
           Font-Bold="True";
           ForeColor="White"
       }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <strong>重写GridView使无数据时显示表头</strong>
    </div>
    <div>
        <TGridView:TGridView DataSourceID="AccessDataSource1" EmptyDataCellCssClass="table" Width="500px" runat="server" ID="TGridView1" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" EnableEmptyContentRender="True" GridLines="Horizontal" AutoGenerateColumns="False">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="UuserID" HeaderText="用户编号" ReadOnly="True" SortExpression="UuserID" />
                <asp:BoundField DataField="UuserName" HeaderText="用户姓名" SortExpression="UuserName" />
                <asp:BoundField DataField="UuserPower" HeaderText="用户类型" SortExpression="UuserPower" />
                <asp:BoundField DataField="Age" HeaderText="年龄" SortExpression="Age" />
                <asp:BoundField DataField="Email" HeaderText="电子邮件" SortExpression="Email" />
                <asp:BoundField DataField="Address" HeaderText="地址" SortExpression="Address" />
            </Columns>
        </TGridView:TGridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/GridViewTest.mdb"
            SelectCommand="SELECT [UuserID], [UuserName], [UuserPower], [Age], [Email], [Address] FROM [Uuser]">
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>
接着是cs文件代码:

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TGridView1.EmptyDataText = "没有数据显示";
    }
}

其实cs文件代码不用也可以的,只要在aspx文件中设置GridView中EmptyDataText 的值就可以了.

方法三上是参照 作者:DotNetEden  转自:http://www.cnblogs.com/zhangronghua/archive/2007/1这篇文章,同时从这篇文章中也学习了,如何编写自定义控件.希望对其他人也有帮助.