程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 在GRIDVIEW中合並單元格

在GRIDVIEW中合並單元格

編輯:.NET實例教程
///     
    ///   合並GridVIEw列中相同的行  
    ///     
    ///   GridVIEw對象  
    ///   需要合並的列  
    public static void GroupRows(GridView GridVIEw1, int cellNum)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridVIEw1.Rows.Count - 1)
        {
            GridViewRow gvr = GridVIEw1.Rows[i];
            for (++i; i < GridVIEw1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridVIEw1.Rows[i];
                if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
          

$False$

        break;
                }

                if (i == GridVIEw1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }

    ///  

  
    ///   根據條件列合並GridVIEw列中相同的行  
    ///     
    ///   GridVIEw對象  
    ///   需要合並的列
    ///   ///   條件列(根據某條件列還合並)
    public static void GroupRows(GridView GridVIEw1, int cellNum, int cellNum2)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridVIEw1.Rows.Count - 1)
        {
            GridViewRow gvr = GridVIEw1.Rows[i];
            for (++i; i < GridVIEw1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridVIEw1.Rows[i];
                if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)
                {
     &nbsp;              gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;

                if (i == GridVIEw1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }

第二個方法只是在第一個方法的修改了一點點, 在判斷單元格的時候,加上了一個條件列:

if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)。當然,第二個方法還可以擴展,根據多個列的條件來合並。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved