• 如何在数据源是空的时候,gridview显示表头(万能)


     

    一般我们用gridview 绑定数据的时候,当数据源是空的时候,通常是写:EmptyDataText="没有任何数据可以显示",这样的话表头是不显示的,下面的代码实现当数据源是空的时候,我们不仅显示表头,而且显示:没有任何数据可以显示,图片在最下面:

    注意:一定要要设

    EmptyDataText="没有任何数据可以显示";然后在.cs文件里面bind()方法里面绑定数据源之后(this.GVpositionrecods.DataBind())之后,把对应的代码复制过去,就OK了。

    下面是代码:

    .aspx

    代码
    <head runat="server">
        
    <title>职位申请记录</title>
        
    <link rel="stylesheet" href="css/common.css" type="text/css" />
        
    <style type="text/css">                  .cssPager span {  color:blue;}             </style>
    </head>
    <body>
        
    <form id="form1" runat="server">
            
    <div id="man_zone">
                
    <center>
                    
    <asp:GridView  AllowSorting="True" ID="GVpositionrecods" runat="server" PageSize="12" AllowPaging="True"
                        EmptyDataText
    ="没有任何数据可以显示" Width="99%" AutoGenerateColumns="False" OnRowDataBound="GVpositionrecods_RowDataBound"
                        OnSelectedIndexChanged
    ="GVpositionrecods_SelectedIndexChanged" OnSelectedIndexChanging="GVpositionrecods_SelectedIndexChanging"
                        OnPageIndexChanging
    ="GVpositionrecods_PageIndexChanging" OnRowCreated="GVpositionrecods_RowCreated" OnSorting="GVpositionrecods_Sorting">
                        
    <Columns>
                            
    <asp:TemplateField HeaderText="职位" SortExpression="zhiwei">
                                
    <ItemTemplate>
                                    
    <a href='throwseeen2.aspx?qiyeid=<%#Eval("qiyeid")%>&id=<%#Eval("ORDER_FORM_ID") %>'>
                                        
    <%#Eval("zhiwei"%>
                                    
    </a>
                                
    </ItemTemplate>
                                
    <ItemStyle HorizontalAlign="Center" />
                            
    </asp:TemplateField>
                            
    <asp:TemplateField HeaderText="企业名称" SortExpression="qiyeid">
                                
    <ItemTemplate>
                                    
    <a href='seeenlist.aspx?getidforen=<%#Eval("qiyeid")%>'>
                                        
    <%#Eval("qiye")%>
                                    
    </a>
                                
    </ItemTemplate>
                                
    <ItemStyle HorizontalAlign="Center" />
                            
    </asp:TemplateField>
                            
    <asp:BoundField DataField="COUNT" HeaderText="人数" SortExpression="COUNT">
                                
    <ItemStyle HorizontalAlign="Center" />
                            
    </asp:BoundField>
                            
    <asp:BoundField DataField="STAR_TIME"  SortExpression="STAR_TIME"  HeaderText="发布时间" DataFormatString="{0:yyyy-MM-dd}"
                                HtmlEncode
    ="False">
                                
    <ItemStyle HorizontalAlign="Center" />
                            
    </asp:BoundField>
                            
    <asp:BoundField DataField="END_TIME" SortExpression="END_TIME" HeaderText="结束时间" DataFormatString="{0:yyyy-MM-dd}"
                                HtmlEncode
    ="False">
                                
    <ItemStyle HorizontalAlign="Center" />
                            
    </asp:BoundField>
                            
    <asp:BoundField DataField="flag" HeaderText="状态">
                                
    <ItemStyle HorizontalAlign="Center" />
                            
    </asp:BoundField>
                        
    </Columns>
                        
    <AlternatingRowStyle BackColor="#E2F8FF"></AlternatingRowStyle>
                        
    <HeaderStyle HorizontalAlign="Center" BackColor="#99CCFF"></HeaderStyle>
                    
    <PagerStyle CssClass="cssPager" />
                    
    </asp:GridView>
                    
    <asp:Label ID="lbcontent" runat="server"></asp:Label>
                
    </center>
            
    </div>
        
    </form>
    </body>

     

     

    .cs

    代码
    public void bind()
        {
            
    //得到对应的简历的ID,从SN里面查看值
            ccwu.Model.T_RESUME_INFO listTemp = (ccwu.Model.T_RESUME_INFO)Session["t_resume_info"];
            ccwu.Model.T_ORDER_RESUME torderresume 
    = new ccwu.Model.T_ORDER_RESUME();
            
    int id = Convert.ToInt32(listTemp.ID.ToString());
            
    //得到对应的简历的ID,从SN里面查看值


            ccwu.DAL.T_JOB jobdal 
    = new ccwu.DAL.T_JOB();
            
    string where = "RESUME_INFO_ID='" + id.ToString() + "'";


            DataSet ds 
    = jobdal.getlistResume(where);

            DataView dv 
    = new DataView(ds.Tables[0]);

            dv.Sort 
    = ViewState["sortExp"].ToString() + " " + ViewState["sortDir"].ToString();

            GVpositionrecods.DataSource 
    = dv;

            
    this.GVpositionrecods.DataBind();


            
    if (this.GVpositionrecods.Rows.Count > 0)
            {

            }
            
    else//如果数据源是空的
            {
                
    //表头的设置
                GridViewRow row = new GridViewRow(-1-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
                
    foreach (DataControlField field in this.GVpositionrecods.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 
    = this.GVpositionrecods.HeaderStyle;
                TableItemStyle emptyStyle 
    = GVpositionrecods.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;
                
    //空白行的设置
                GridViewRow row1 = new GridViewRow(0-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
             
                    TableCell cell1 
    = new TableCell();

                    cell1.Text 
    = "没有任何数据可以显示";
                    cell1.BackColor 
    = System.Drawing.Color.White;
                    row1.Cells.Add(cell1);
                    cell1.ColumnSpan 
    = 6;   
              

                
    if (this.GVpositionrecods.Controls.Count == 0)
                {
                    GVpositionrecods.Page.Response.Write(
    "<script language='javascript'>alert('必须在初始化表格类之前执行DataBind方法并设置EmptyDataText属性不为空!');</script>");
                }
                
    else
                {
                    GVpositionrecods.Controls[
    0].Controls.Clear();
                    
    this.GVpositionrecods.Controls[0].Controls.AddAt(0, row);
                    
    this.GVpositionrecods.Controls[0].Controls.AddAt(1, row1);
                }
            }

        }

    知识点1

    GridViewRowDataRowView完全就是两个不相干的东西 是两个级别的事情 一个是控件级别的 一个是数据级别的

    GridViewRow

      MSDN解释:呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时就执行一个自定义例程(如在行中添加自定义内容)。

    使用 RowCreated 事件将正在创建的行的索引存储在该行所包含 LinkButton 控件的 CommandArgument 属性中;(每当创建一行的时候,执行RowCreated事件里面的代码)

     

    知识点2(在有页码的时候,当前页码变大,且颜色也变)

     

    用样式显示当前页码;

        <style type="text/css">                  .cssPager span {  color:blue;}             </style>

     

      <PagerStyle CssClass="cssPager" />

     

  • 相关阅读:
    flash时间控制
    内联元素及其特性 &块元素
    清除浮动两种方法
    ie6 lineheight失效 待测试
    表单 文字和输入框对齐的最简单的方法
    ie6bugie6默认的最小高度是设定字体的高度
    CSS triangle generator
    解释性的语言vs编译性语言
    outerHeight(options)
    获取input的value值得 jQuery插件
  • 原文地址:https://www.cnblogs.com/muer/p/1697178.html
Copyright © 2020-2023  润新知