gridview无数据行时显示表头的方法

//使用方法:
//在page_load事件中加入下列样式代码,其中grdPay为GridView. 必须设置grdPay的EmptyDataText属性不为空.
        if (this.IsPostBack == false)
        {
                grdPay.DataBind();
        }
        UGridView grd = new UGridView(grdPay);

//源程序
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  class UGridView
{
    public  UGridView(GridView grd)
    {
        if (grd.EmptyDataText == "")
        {
            grd.EmptyDataText = "没有符合条件的数据";
        }
        grd.PreRender += new EventHandler(grd_PreRender);       
    }

    void grd_PreRender(object sender, EventArgs e)
    {
        DrawHeader(sender);
    }
    private void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1)
        {
            DrawHeader(sender);
        }
    }
    private void DrawHeader(object sender)
    {
        GridView grd = (GridView)sender;
        if (grd.Rows.Count > 0) return; //有数据,不要处理
        if (grd.DataSource != null)
        {
            if (((DataTable)grd.DataSource).Rows.Count > 0)
            {
                return;
            }
        }
        GridViewRow row = new GridViewRow(-1, -1,DataControlRowType.EmptyDataRow,DataControlRowState.Normal);
        foreach (DataControlField field in grd.Columns)
        {
            TableCell cell = new TableCell();
            cell.Text = field.HeaderText;
            cell.Width = field.HeaderStyle.Width;
            cell.Height = field.HeaderStyle.Height;
            cell.ForeColor = field.HeaderStyle.ForeColor;
            cell.Font.Size = field.HeaderStyle.Font.Size;
            cell.Font.Bold = field.HeaderStyle.Font.Bold;
            cell.Font.Name = field.HeaderStyle.Font.Name;
            cell.Font.Strikeout = field.HeaderStyle.Font.Strikeout;
            cell.Font.Underline = field.HeaderStyle.Font.Underline;
            cell.BackColor = field.HeaderStyle.BackColor;
            cell.VerticalAlign = field.HeaderStyle.VerticalAlign;
            cell.HorizontalAlign = field.HeaderStyle.HorizontalAlign;
            cell.CssClass = field.HeaderStyle.CssClass;
            cell.BorderColor = field.HeaderStyle.BorderColor;
            cell.BorderStyle = field.HeaderStyle.BorderStyle;
            cell.BorderWidth = field.HeaderStyle.BorderWidth;
            row.Cells.Add(cell);               
        }    
      
        TableItemStyle headStyle = grd.HeaderStyle;
        TableItemStyle emptyStyle = grd.EmptyDataRowStyle;
        emptyStyle.Width = headStyle.Width;
        emptyStyle.Height = headStyle.Height;
        emptyStyle.ForeColor = headStyle.ForeColor;
        emptyStyle.Font.Size = headStyle.Font.Size;
        emptyStyle.Font.Bold = headStyle.Font.Bold;
        emptyStyle.Font.Name = headStyle.Font.Name;
        emptyStyle.Font.Strikeout = headStyle.Font.Strikeout;
        emptyStyle.Font.Underline = headStyle.Font.Underline;
        emptyStyle.BackColor = headStyle.BackColor;
        emptyStyle.VerticalAlign = headStyle.VerticalAlign;
        emptyStyle.HorizontalAlign = headStyle.HorizontalAlign;
        emptyStyle.CssClass = headStyle.CssClass;
        emptyStyle.BorderColor = headStyle.BorderColor;
        emptyStyle.BorderStyle = headStyle.BorderStyle;
        emptyStyle.BorderWidth = headStyle.BorderWidth;
        if (grd.Controls.Count == 0)
        {
            grd.Page.Response.Write("<script language='javascript'>alert('必须在初始化表格类之前执行DataBind方法并设置EmptyDataText属性不为空!');</script>");
        }
        else
        {
            grd.Controls[0].Controls.Clear(); //删除没数据时的提示
            grd.Controls[0].Controls.AddAt(0, row);
        }          

    }

}

转载于:https://www.cnblogs.com/linux-withwind/archive/2007/03/22/684180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值