首页 > 其他分享 >gridview无数据行时显示表头的方法

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

时间:2023-11-09 12:01:05浏览次数:26  
标签:GridView 数据源 writer 表头 行时 gridview msgCell 空时 row

 

GridView当数据源为空时如何实现显示表头

问题:asp.net 2.0 中引入的GridView控件当其数据源为空时(GridView.DataSource=null)不能显示
出表头.
解决:
方法一:采用其EmptyTemplate来实现,模版中写一个静态的table;
缺点: 麻烦,每个GridVIew都需要设置一下.
方法二: 若数据源为DataTable,则当无数据时,始终返回一个空行的DataTable;
若数据源是集合类(ArrayList,List<T>等),无数据时,生成一个空的实体,加入到集合类中.
缺点: 还是麻烦.
方法三:
也是要给大家介绍的方法: 扩展GridView来实现.继承GridVie,重写Render方法,当其数据源为空时做一下处理,直接看代码吧:

/// <summary>
    /// GridView 扩展控件
    /// </summary>
     public class GridView : System.Web.UI.WebControls.GridView
    {       
        private bool _enableEmptyContentRender = true ;
/// <summary>
/// 是否数据为空时显示标题行
/// </summary>
        public bool EnableEmptyContentRender
        {
            set { _enableEmptyContentRender = value; }
            get { return _enableEmptyContentRender; }
        }
        private string _EmptyDataCellCssClass ;
/// <summary>
/// 为空时信息单元格样式类
/// </summary>
        public string EmptyDataCellCssClass
        {
            set { _EmptyDataCellCssClass = value ; }
            get { return _EmptyDataCellCssClass ; }
        }
/// <summary>
/// 为空时输出内容
/// </summary>
/// <param name="writer"></param>
        protected virtual void RenderEmptyContent(HtmlTextWriter writer)
        {
            Table t = new Table(); //create a table
            t.CssClass = this.CssClass; //copy all property
            t.GridLines = this.GridLines;
            t.BorderStyle = this.BorderStyle;
            t.BorderWidth = this.BorderWidth;
            t.CellPadding = this.CellPadding;
            t.CellSpacing = this.CellSpacing;
            t.HorizontalAlign = this.HorizontalAlign;
            t.Width = this.Width;
            t.CopyBaseAttributes(this);
            TableRow row = new TableRow();
            t.Rows.Add(row);
            foreach (DataControlField f in this.Columns) //generate table header
            {
                TableCell cell = new TableCell();
                cell.Text = f.HeaderText;
                cell.CssClass = "TdHeaderStyle1"; //这里把表头样式写死了
                row.Cells.Add(cell);
            }
            TableRow row2 = new TableRow();
            t.Rows.Add(row2);
            TableCell msgCell = new TableCell();
            msgCell.CssClass = this._EmptyDataCellCssClass;
if (this.EmptyDataTemplate != null) //the second row, use the template
            {
this.EmptyDataTemplate.InstantiateIn(msgCell);
            }
else //the second row, use the EmptyDataText
            {
                msgCell.Text = this.EmptyDataText;
            }
            msgCell.HorizontalAlign = HorizontalAlign.Center;
            msgCell.ColumnSpan = this.Columns.Count;
            row2.Cells.Add(msgCell);
            t.RenderControl(writer);
       }
        protected override void  Render(HtmlTextWriter writer)
        {
if ( _enableEmptyContentRender && ( this.Rows.Count == 0 || this.Rows[0].RowType == DataControlRowType.EmptyDataRow) )
            {
                RenderEmptyContent(writer);
            }
else
            {
                base.Render(writer);
            }
        }    
    }
}

REF:

http://mattberseth2.com/demo/Default.aspx?Name=How+To%3a+Show+Header+and+Footer+Rows+in+an+Empty+GridView&Filter=All





标签:GridView,数据源,writer,表头,行时,gridview,msgCell,空时,row
From: https://blog.51cto.com/emanlee/8275556

相关文章

  • vue3 使用elementUI饿了么el-table组件 动态循环自定义表头列数据
     在vue3上使用el-table组件自定义循环表头列;<el-table:data="list"v-loading="loading"border>      <!--@selection-change="handleSelectionChange"-->      <!--<el-table-columntype="selection"wi......
  • GridView中的更新按钮不能触发RowUpdating事件
    当点击“编辑”按钮以后,可以看到“更新”和“取消”按钮,“取消”按钮可以正常触发RowCancelingEdit事件,但是“更新”按钮不能触发RowUpdating事件。解决方案:在<asp:CommandField>中添加CausesValidation="false"。 GridviewRowUpdatingNotFiring(RowUpdatingeventnotfirin......
  • textbox的textmode取为multiline多行时,其maxlength不起作用
    方法一: 验证控件验证(经实践可行)SettingtheMaxengthofaTextBoxwhenitisinMultiline,YoucanuseRegularExpressionValidatorcontrolasshownbelow <asp:TextBoxID="txtConclusion"MaxLength="200"TextMode="MultiLine"Height="......
  • DataGridView的AutoGenerateColumns控制显示列
    在用C#的EF框架进行数据显示的时候出现了DataGridView显示未编辑列的问题,后来发现通过对DataGridView的 AutoGenerateColumns属性进行定义借可以解决AutoGenerateColumns属性默认未true,此时会显示所有,更改为false就可以了(此属性只能后台操控,代码加载main方法里)this.UsersDGV.......
  • GridView根据某行的内容显示或隐藏Edit按钮
     ProtectedSubgdv_RowDataBound(ByValsenderAsObject,ByValeAsGridViewRowEventArgs)Handlesgdv.RowDataBoundIfe.Row.RowType=DataControlRowType.DataRowThenIfCType(e.Row.FindControl("lblVerifiedBy"),Label).T......
  • Flutter的GridView控件
    简介GridView是Flutter中用于创建网格布局的强大小部件。它允许你在行和列中排列子小部件,非常适合显示大量项目,例如图像、文本、卡片等。使用详解以下是关于如何使用GridView控件的详细讲解:导入依赖项在你的Flutter项目中,首先确保已经导入了flutter/material.dart包,......
  • Creating HTML table with vertically oriented text as table header 表头文字方向
    ASanoldquestion,thisismorelikeinfoorreminderaboutverticalmarginorpaddingin%thattakesparent'swidthasreference.Ifyouuseapseudoelementandvertical-padding,youmaybasiclydrawasquareboxor<td>:http://jsfiddle.n......
  • ZPN项目 3:使用 Intel ICX 编译多线程动态运行时pyd的失败记录
    使用Pybind11,可以生成供python调用接口的C++动态链接库,该库包含一个符合python启动签名的函数,并以pyd为后缀。编译生成该库,与生成一般C++库基本一致。特殊地,当采用以下全部配置生成时,程序的调用将失败:使用icx-cl编译器(兼容MSVC编译器标识的icx编译器)使用多线程动态运行时标......
  • linux 输入长命令行时不会自动换行只会回到行首,并且覆盖前面的内容。解决办法。
    CustomizeCode\e[Begincolorchanges\e[0mExitcolor-changemode0;32mSpecifythecolormodeThefirstnumberinthecolorcodespecifiesthetypeface:•0–Normal•1–Bold(bright)•2–Dim•4–UnderlinedThesecondnumberindicatesthecol......
  • Windows系统 C/C++程序编译后首次执行时间很长 断网则正常执行 的解决方法
    Windows系统C/C++程序编译后首次执行时间很长断网则正常执行的解决方法问题描述运行环境:Win10、Win11或其他Win环境。在各类IDE(包括但不限于VC6/VisualStuido等)编译任意C/C++源码(无论该程序有多简单),首次运行时间异常地长,即在黑窗口无任何输出。等待一段时间后有程序正......