GridView、DataGrid 表头合并、表体合并[合并相邻行相同数据的单元格]

 

  1 using  System;
  2 using  System.Data;
  3 using  System.Configuration;
  4 using  System.Web;
  5 using  System.Web.Security;
  6 using  System.Web.UI;
  7 using  System.Web.UI.WebControls;
  8 using  System.Web.UI.WebControls.WebParts;
  9 using  System.Web.UI.HtmlControls;
 10
 11 public   partial   class  _Default : System.Web.UI.Page 
 12 {
 13    protected void Page_Load(object sender, EventArgs e)
 14    {
 15        DataTable DT = new DataTable();
 16        DT.Columns.Add("序号");
 17        DT.Columns.Add("原油部分");
 18        DT.Columns.Add("轻烃部分");
 19        DataRow dr = DT.NewRow();
 20        dr[0= "1";
 21        dr[1= "2";
 22        dr[2= "3";
 23        DT.Rows.Add(dr);
 24        
 25        GridView1.DataSource = DT;
 26        GridView1.DataBind();
 27    }

 28
 29    /// <summary>
 30    /// 合并相邻行相同数据的单元格
 31    /// </summary>
 32    /// <param name="DataGrid1">DataGrid对象</param>
 33    /// <param name="ColNum">要合并的列数</param>

 34    private void Span(GridView DataGrid1, int ColNum)
 35    {
 36        string temp = "";
 37        int j = 0, intspan;
 38        int local = 0;
 39        for (int i = 0; i < DataGrid1.Rows.Count; i++)
 40        {
 41            temp = DataGrid1.Rows[i].Cells[ColNum].Text;
 42            local = i;
 43            intspan = 1;
 44            for (j = j + 1; j < DataGrid1.Rows.Count; j++)
 45            {
 46                if (string.Compare(temp, DataGrid1.Rows[j].Cells[ColNum].Text) == 0)
 47                {
 48                    intspan++;
 49                    DataGrid1.Rows[local].Cells[ColNum].RowSpan = intspan;
 50                    DataGrid1.Rows[j].Cells[ColNum].Visible = false;
 51                }

 52                else
 53                {
 54                    temp = DataGrid1.Rows[j].Cells[ColNum].Text;
 55                    local = j;
 56                    intspan = 1;
 57                }

 58            }

 59        }

 60    }

 61
 62    /// <summary>
 63    /// 合并相邻行相同数据的单元格
 64    /// </summary>
 65    /// <param name="DataGrid1">DataGrid对象</param>
 66    /// <param name="ColNum">要合并的列数</param>

 67    private void Span(DataGrid DataGrid1, int ColNum)
 68    {
 69        string temp = "";
 70        int j = 0, intspan;
 71        int local = 0;
 72        for (int i = 0; i < DataGrid1.Items.Count; i++)
 73        {
 74            temp = DataGrid1.Items[i].Cells[ColNum].Text;
 75            local = i;
 76            intspan = 1;
 77            for (j = j + 1; j < DataGrid1.Items.Count; j++)
 78            {
 79                if (string.Compare(temp, DataGrid1.Items[j].Cells[ColNum].Text) == 0)
 80                {
 81                    intspan++;
 82                    DataGrid1.Items[local].Cells[ColNum].RowSpan = intspan;
 83                    DataGrid1.Items[j].Cells[ColNum].Visible = false;
 84                }

 85                else
 86                {
 87                    temp = DataGrid1.Items[j].Cells[ColNum].Text;
 88                    local = j;
 89                    intspan = 1;
 90                }

 91            }

 92        }

 93    }

 94    /// <summary>
 95    /// 合并表头表体
 96    /// </summary>
 97    /// <param name="sender"></param>
 98    /// <param name="e"></param>

 99    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
100    {
101        if (e.Row.RowType == DataControlRowType.Header)
102        {
103            for (int i = 0; i < e.Row.Cells.Count; i++)
104            {
105                if (i == 0)
106                    e.Row.Cells[i].RowSpan = 3;
107                if (i == 1)
108                    e.Row.Cells[i].ColumnSpan = 3;
109                if (i == 2)
110                    e.Row.Cells[i].ColumnSpan = 2;
111            }

112            TableCell cell = e.Row.Cells[e.Row.Cells.Count - 1];
113            LiteralControl lc = new LiteralControl(cell.Text + "</td></tr>" + GetMegCell());
114            cell.Controls.Add(lc);
115        }

116        else if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.EmptyDataRow)
117        {
118            e.Row.Cells.AddAt(3, tc("3"));
119            e.Row.Cells.AddAt(4, tc("4"));
120            e.Row.Cells.AddAt(5, tc("5"));
121        }

122    }

123
124    private TableCell tc(string va)
125    {
126        TableCell tc1 = new TableCell();
127        tc1.Text = va;
128        return tc1;
129    }

130    /// <summary>
131    /// 欲合并后的表头HTML
132    /// </summary>
133    /// <returns></returns>

134    private string GetMegCell()
135    {
136        return "<tr><td width=189 colspan=2 align=\"center\"><b>轻烃计算</b></td><td width=95 rowspan=2 align=center><b>合计</b></td><td width=95 align=center><b>采气部分</b></td><td width=95 align=center><b>销售部分</b></td></tr><tr><td width=95 align=center><b>日计算</b></td><td width=95 align=center><b>月计算</b></td><td width=95 align=center><b>日数据</b></td><td width=95 align=center><b>日数据</b></td>";
137    }

138}

139

你可能感兴趣的:(datagrid)