扩展 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;
        }

    }

}


转载于:https://www.cnblogs.com/RuiLei/archive/2008/05/24/1206352.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值