擴展 GridView 控件 - 無數據時顯示標題列

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

        }

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章