小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Gridview創(chuàng)建頭

 實力決定地位 2010-12-08
方法一:
效果截圖,第一行是通過代碼添加的,
第二行GridView本身的表頭
在GridView的RowCreated事件中寫添加代碼
  1. protected void SmartGridView1_RowCreated(object sender, GridViewRowEventArgs e)
  2. {
  3. if (e.Row.RowType == DataControlRowType.Header)
  4. {
  5. GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);//表頭行
  6. TableHeaderCell cell = new TableHeaderCell();
  7. cell.ColumnSpan = 5;
  8. rowHeader.Cells.Add(cell);
  9. cell = new TableHeaderCell();
  10. cell.Text = Resources.Coding.Revenue;
  11. cell.ColumnSpan = 5;
  12. rowHeader.Cells.Add(cell);
  13. cell = new TableHeaderCell();
  14. cell.Text = Resources.Coding.Cost;
  15. cell.ColumnSpan = 5;
  16. rowHeader.Cells.Add(cell);
  17. rowHeader.Cells.Add(new TableHeaderCell());
  18. ((GridView)sender).Controls[0].Controls.AddAt(0, rowHeader);//加到最前面
  19. }
  20. }
 
 
 
方法二:
實現(xiàn)方法就是給單元格填充我們想要的格式代碼。
 
在行創(chuàng)建事件中加如下代碼:
protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e )
  {

    
if (e.Row.RowType == DataControlRowType.Header)
    {
      
//創(chuàng)建一個GridViewRow,相當于表格的 TR 一行
      GridViewRow rowHeader = new GridViewRow(00, DataControlRowType.Header, DataControlRowState.Normal);
      string HeaderBackColor 
= "#EDEDED";
      rowHeader.BackColor 
= System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);

      
//實現(xiàn)確定要顯示的表頭樣式,也可以通過計算生成

      
//    <tr>
      //      <td rowspan='2'>關(guān)鍵單元格</td>
      //      <td colspan='2'>表頭文字</td>
      //      <td colspan='2'>表頭文字</td>
      //      <td>表頭文字</td>
      //      </tr>
      //      <tr bgcolor='#FFF'>
      //      <td colspan='2'>表頭文字</td>
      //      <td rowspan='2'>表頭文字</td>
      //      <td colspan='2'>表頭文字</td>
      //      </tr>
      //      <tr bgcolor='#FFF'>
      //      <td>表頭文字</td>
      //      <td>表頭文字</td>
      //      <td>表頭文字</td>
      //      <td>表頭文字</td>
      //      <td>表頭文字</td>";
      //   </tr>
      // 上面的樣式可以設(shè)置斜線

      Literal newCells 
= new Literal();
      newCells.Text 
= @"表頭文字1</th>
                  <th colspan='2'>表頭文字2</th>
                  <th colspan='2'>表頭文字3</th>
                  <th>表頭文字4</th>
                  </tr>
                  <tr bgcolor='
" + HeaderBackColor + "'>";
      newCells.Text 
+= @"                         
                  <th colspan='2'>表頭文字5</th>
                  <th rowspan='2'>表頭文字6</th>
                  <th colspan='2'>表頭文字7</th>
                  </tr>
                  <tr bgcolor='
" + HeaderBackColor + "'>";
      newCells.Text 
+= @"  
                  <th>表頭文字8</th>
                  <th>表頭文字9</th>
                  <th>表頭文字10</th>
                  <th>表頭文字11</th>
                  <th>表頭文字12
";

      TableCellCollection cells 
= e.Row.Cells;
      TableHeaderCell headerCell 
= new TableHeaderCell();
      
//下面的屬性設(shè)置與 <td rowspan='2'>關(guān)鍵單元格</td> 要一致
      headerCell.RowSpan = 2;
      headerCell.Controls.Add(newCells);
      rowHeader.Cells.Add(headerCell);

      rowHeader.Cells.Add(headerCell);
      rowHeader.Visible 
= true;

      
//添加到 GridView1
      GridView1.Controls[0].Controls.AddAt(0, rowHeader);
    }
  }
 
 
方法三(此方法用的一個類文件即可):
 
表頭定義說明:
        表頭定義方法:相鄰父列頭之間用''#''分隔,上級行與下級行用空格('' '')分隔,相鄰未級子列頭用逗號分隔('','').
表頭定義示例:
A.兩層
煙葉等級#級別#保山市 保山,小計#楚雄州 姚安,小計#大理州 賓川,大理,小計#紅河州 建水,瀘西,彌勒,石屏,小計#昆明市 富民,祿勸,小計#麗江市 華坪,寧蒗,小計#臨滄市 滄源,鳳慶,耿馬,臨翔,雙江,永德,云縣,鎮(zhèn)康,小計#普洱市 景東,思茅,鎮(zhèn)沅,小計#曲靖市 富源,陸良,羅平,師宗,宣威,小計#文山州 文山,小計#玉溪市 玉溪,小計#總計
B.三層
等級#級別#上期結(jié)存 件數(shù),重量,比例#本期調(diào)入 收購調(diào)入 件數(shù),重量,比例#本期發(fā)出 車間投料 件數(shù),重量,比例#本期發(fā)出 產(chǎn)品外銷百分比 件數(shù),重量,比例#平均值
調(diào)用說明:使用時在GridView的RowCreated事件中加入下面代碼調(diào)用
 


        if (e.Row.RowType == DataControlRowType.Header)
        
{
            DynamicTHeaderHepler dHelper 
= new DynamicTHeaderHepler();
            
string header = "等級#級別#上期結(jié)存 件數(shù),重量,比例#本期調(diào)入 收購調(diào)入 件數(shù),重量,比例#本期發(fā)出 車間投料 件數(shù),重量,"
                          
+ "比例#本期發(fā)出 產(chǎn)品外銷百分比 件數(shù),重量,比例#平均值";

            dHelper.SplitTableHeader(e.Row, header);
        }
 
類文件如下:
 
//
//***********************************************************************
//  Created: 2007-10-29    Author:  ruijc
//  File: DynamicTHeaderHepler.cs
//  Description: 動態(tài)生成復合表頭幫助類
//  相鄰父列頭之間用'#'分隔,父列頭與子列頭用空格(' ')分隔,相鄰子列頭用逗號分隔(',').
//***********************************************************************
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
public class DynamicTHeaderHepler
{
    public DynamicTHeaderHepler()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    /**//// <summary>
    /// 重寫表頭
    /// </summary>
    /// <param name="targetHeader">目標表頭</param>
    /// <param name="newHeaderNames">新表頭</param>
    /// <remarks>
    /// 等級#級別#上期結(jié)存 件數(shù),重量,比例#本期調(diào)入 收購調(diào)入 件數(shù),重量,比例#本期發(fā)出 車間投料 件數(shù),重量,
    /// 比例#本期發(fā)出 產(chǎn)品外銷百分比 件數(shù),重量,比例#平均值
    /// </remarks>
    public void SplitTableHeader(GridViewRow targetHeader, string newHeaderNames)
    {
        TableCellCollection tcl = targetHeader.Cells;//獲得表頭元素的實例
        tcl.Clear();//清除元素
        int row = GetRowCount(newHeaderNames);
        int col = GetColCount(newHeaderNames);
        string[,] nameList = ConvertList(newHeaderNames,row,col);
        int RowSpan = 0;
        int ColSpan = 0;
        for (int k = 0; k < row; k++)
        {
            string LastFName = "";
            for (int i = 0; i < col; i++)
            {
                if (LastFName == nameList[i, k] && k!=row-1)
                {
                    LastFName = nameList[i, k];
                    continue;
                }
                else
                {
                    LastFName = nameList[i, k];
                }
                int bFlag=IsVisible(nameList, k, i, LastFName);
                switch (bFlag)
                {
                    case 0:
                        break;
                    case 1:
                        RowSpan = GetSpanRowCount(nameList,row, k, i);
                        ColSpan = GetSpanColCount(nameList,row,col, k, i);
                        tcl.Add(new TableHeaderCell());//添加表頭控件
                        tcl[tcl.Count - 1].RowSpan = RowSpan;
                        tcl[tcl.Count - 1].ColumnSpan = ColSpan;
                        tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
                        tcl[tcl.Count - 1].Text = LastFName;
                        break;
                    case -1:
                        string[] EndColName = LastFName.Split(new char[] { ',' });
                        foreach(string eName in EndColName){
                            tcl.Add(new TableHeaderCell());//添加表頭控件
                            tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
                            tcl[tcl.Count - 1].Text = eName;
                        }
                        break;
                }
            }
            if (k != row-1)
            {//不是起始行,加入新行標簽
                tcl[tcl.Count - 1].Text = tcl[tcl.Count - 1].Text+"</th></tr><tr>";
            }
        }
    }
    /**//// <summary>
    /// 如果上一行已經(jīng)輸出和當前內(nèi)容相同的列頭,則不顯示
    /// </summary>
    /// <param name="ColumnList">表頭集合</param>
    /// <param name="rowIndex">行索引</param>
    /// <param name="colIndex">列索引</param>
    /// <returns>1:顯示,-1:含','分隔符,0:不顯示</returns>
    private int IsVisible(string[,] ColumnList,int rowIndex, int colIndex,string CurrName)
    {
        if (rowIndex!=0){
            if (ColumnList[colIndex,rowIndex-1]==CurrName){
                return 0;
            }else{
                if (ColumnList[colIndex, rowIndex].Contains(","))
                {
                    return -1;
                }else{
                    return 1;
                }
            }
        }
        return 1;
    }
    /**//// <summary>
    /// 取得和當前索引行及列對應的下級的內(nèi)容所跨的行數(shù)
    /// </summary>
    /// <param name="ColumnList">表頭集合</param>
    /// <param name="row">行數(shù)</param>
    /// <param name="rowIndex">行索引</param>
    /// <param name="colIndex">列索引</param>
    /// <returns>行數(shù)</returns>
    private int GetSpanRowCount(string[,] ColumnList, int row,int rowIndex, int colIndex)
    {
        string LastName = "";
        int RowSpan = 1;
        for (int k = rowIndex; k < row; k++)
        {
            if (ColumnList[colIndex,k]==LastName){
                RowSpan++;
            }else{
                LastName = ColumnList[colIndex, k];
            }
        }
        return RowSpan;
    }
    /**//// <summary>
    /// 取得和當前索引行及列對應的下級的內(nèi)容所跨的列數(shù)
    /// </summary>
    /// <param name="ColumnList">表頭集合</param>
    /// <param name="row">行數(shù)</param>
    /// <param name="col">列數(shù)</param>
    /// <param name="rowIndex">行索引</param>
    /// <param name="colIndex">列索引</param>
    /// <returns>列數(shù)</returns>
    private int GetSpanColCount(string[,] ColumnList,int row, int col,int rowIndex, int colIndex)
    {
        string LastName = ColumnList[colIndex, rowIndex] ;
        int ColSpan = ColumnList[colIndex, row-1].Split(new char[] { ',' }).Length;
        ColSpan = ColSpan == 1 ? 0 : ColSpan;
        for(int i=colIndex+1;i<col;i++)
        {
            if (ColumnList[i, rowIndex] == LastName)
            {
                ColSpan += ColumnList[i, row - 1].Split(new char[] { ',' }).Length;
            }
            else
            {
                LastName = ColumnList[i, rowIndex];
                break;
            }
        }
        return ColSpan;
    }
    /**//// <summary>
    /// 將已定義的表頭保存到數(shù)組
    /// </summary>
    /// <param name="newHeaders">新表頭</param>
    /// <param name="row">行數(shù)</param>
    /// <param name="col">列數(shù)</param>
    /// <returns>表頭數(shù)組</returns>
    private string[,] ConvertList(string newHeaders, int row, int col)
    {
        string[] ColumnNames = newHeaders.Split(new char[] {'#'});
        string[,] news = new string[col, row];
        string Name = "";
        for (int i = 0; i < col; i++)
        {
            string[] CurrColNames = ColumnNames[i].ToString().Split(new char[] { ' ' });
            for (int k = 0; k < row; k++)
            {
                if (CurrColNames.Length - 1 >= k)
                {
                    if (CurrColNames[k].Contains(","))
                    {
                        if (CurrColNames.Length != row)
                        {
                            if (Name == "")
                            {
                                news[i, k] = news[i, k - 1];
                                Name = CurrColNames[k].ToString();
                            }
                            else
                            {
                                news[i, k + 1] = Name;
                                Name = "";
                            }
                        }else{
                            news[i, k] = CurrColNames[k].ToString();
                        }
                    }else{
                        news[i, k] = CurrColNames[k].ToString();
                    }
                }else{
                    if (Name == "")
                    {
                        news[i, k] = news[i, k - 1];
                    }else{
                        news[i, k] = Name;
                        Name = "";
                    }
                }
            }
        }
        return news;
    }
    /**//// <summary>
    /// 取得復合表頭的行數(shù)
    /// </summary>
    /// <param name="newHeaders">新表頭</param>
    /// <returns>行數(shù)</returns>
    private int GetRowCount(string newHeaders)
    {
        string[] ColumnNames = newHeaders.Split(new char[] { '#' });
        int Count = 0;
        foreach(string name in ColumnNames){
            int TempCount = name.Split(new char[] { ' ' }).Length;
            if (TempCount > Count)
                Count = TempCount;
        }
        return Count;
    }
    /**//// <summary>
    /// 取得復合表頭的列數(shù)
    /// </summary>
    /// <param name="newHeaders">新表頭</param>
    /// <returns>列數(shù)</returns>
    private int GetColCount(string newHeaders)
    {
        return newHeaders.Split(new char[] { '#' }).Length;
    }
  
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多