zoukankan      html  css  js  c++  java
  • 扩展 GridView 控件 无数据时显示标题列 : C# Version

    原文源地址:扩展 GridView 控件 - 无数据时显示标题列 :VB.net Version

    此文章,我一看觉得在平时还是比较有用的。所以做一下C# Convert VB.net

    (仅针对于新手,需要者浏览)

    Page Code

     
    <gvExtrend:GridViewEmptyHeader ID="gvEmptyHeader" runat="server" AutoGenerateColumns="False"
                EmptyShowHeader
    ="True" EmptyDataText="Empty Data" AllowPaging="True" 
                DataKeyNames
    ="ProductID" DataSourceID="SqlDataSource1">
                
    <Columns>
                    
    <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
                        SortExpression
    ="ProductID" />
                    
    <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                        SortExpression
    ="ProductName" />
                    
    <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" 
                        SortExpression
    ="SupplierID" />
                
    </Columns>
            
    </gvExtrend:GridViewEmptyHeader>
            
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString
    ="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" 
                ProviderName
    ="System.Data.SqlClient" 
                SelectCommand
    ="SELECT [ProductID], [ProductName], [SupplierID] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
                
    <SelectParameters>
                    
    <asp:Parameter DefaultValue="99999" Name="CategoryID" Type="Int32" />
                
    </SelectParameters>
            
    </asp:SqlDataSource>

    Web.Config 添加注册Tag

    <pages>
       
    <controls>
             
    <add tagPrefix="gvExtrend" namespace="GridViewExtrend" assembly="GridViewExtrend"/>
       
    </controls>
    </pages>

    GridViewExtrend Source

    namespace GridViewExtrend
    {
        
    using System.Web.UI.WebControls;
        
    using System;
        
    using System.Collections.Generic;
        
    using System.ComponentModel;
        
    using System.Text;
        
    using System.Web.UI;
        
    using System.Drawing;
        
    using System.Collections;

        [Description(
    "GridViewEmptyHeader"), ToolboxData("<{0}:GvEmptyHeader runat=server><{0}:GvEmptyHeader>")]
        
    public class GridViewEmptyHeader : GridView
        
    {
            
    private Boolean fEmptyShowHeader = true;

            
    /// <summary>
            
    /// 无数据时是否显示字段标题
            
    /// </summary>

            public Boolean EmptyShowHeader
            
    {
                
    get
                
    {
                    
    return fEmptyShowHeader;
                }

                
    set
                
    {
                    fEmptyShowHeader 
    = value;
                }

            }


            
    /// <summary>
            
    /// 建立子控件
            
    /// </summary>
            
    /// <param name="dataSource"></param>
            
    /// <param name="dataBinding"></param>
            
    /// <returns>建立的数据列数目</returns>

            protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
            
    {
                Int32 rowCount;
                Table table 
    = new Table();

                rowCount 
    = base.CreateChildControls(dataSource, dataBinding);

                
    if (this.fEmptyShowHeader && (rowCount == 0))
                
    {
                    table 
    = CreateEmptyTable();
                    Controls.Clear();
                    Controls.Add(table);
                }


                
    return rowCount;
            }


            
    private Table CreateEmptyTable()
            
    {
                Table table 
    = new Table();
                GridViewRow gridViewRow;
                TableCell cell 
    = new TableCell();
                Int32 count;
                GridViewRowEventArgs e;

                table 
    = base.CreateChildTable();
                count 
    = this.Columns.Count - 1;

                
    //Create Title Columns
                gridViewRow = base.CreateRow(-1-1, DataControlRowType.Header, DataControlRowState.Normal);
                DataControlField[] fields 
    = new DataControlField[count + 1];
                
    this.Columns.CopyTo(fields, 0);
                
    this.InitializeRow(gridViewRow, fields);
                e 
    = new GridViewRowEventArgs(gridViewRow);
                
    this.OnRowCreated(e);
                table.Rows.Add(gridViewRow);

                
    //Create Empty DataColumns
                gridViewRow = new GridViewRow(-1-1, DataControlRowType.DataRow, DataControlRowState.Normal);
                cell.ColumnSpan 
    = fields.Length;
                cell.Width 
    = Unit.Percentage(100);
                cell.Text 
    = this.EmptyDataText;
                cell.HorizontalAlign 
    = HorizontalAlign.Center;
                gridViewRow.Cells.Add(cell);
                table.Rows.Add(gridViewRow);

                
    return table;
            }

        }

    }


  • 相关阅读:
    写给想要做自动化测试的人(更新篇)
    testng 教程之使用参数的一些tricks配合使用reportng
    testng教程之testng.xml的配置和使用,以及参数传递
    testng 教程
    博客园计算园龄不正确,请管理员确认
    selenium 总结篇,常见方法和页面元素的操作
    测试登录界面
    在测试框架中使用Log4J 2
    泡泡一分钟:Geometric and Physical Constraints for Drone-Based Head Plane Crowd Density Estimation
    Look Further to Recognize Better: Learning Shared Topics and Category-Specific Dictionaries for Open-Ended 3D Object Recognition
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1206352.html
Copyright © 2011-2022 走看看