//单元格要合并的行数
    public int intRowSpan = 1;
    //开始合并的索引值
    public int intIndex = 0;
    //记录第一列相等的条件
    private string strText = "";
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == 0)
        {
            strText = e.Row.Cells[0].Text;
        }
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != 0)
        {
            if (e.Row.Cells[0].Text == strText)
            {
                intRowSpan++;
                GridView1.Rows[intIndex].Cells[0].RowSpan = intRowSpan;
                e.Row.Cells[0].Visible = false;
            }
            else
            {
                intRowSpan = 1;
                intIndex = e.Row.RowIndex;
            }
            strText = e.Row.Cells[0].Text;
        }    }
上面这段代码是实现gridview合并行的代码,代码是可以用的,我想把void GridView1_RowDataBound事件里的写成一个方法,以后可以直接调用了,因为我有可能第一列、第二列、第三列的行也要合并,不可能合一次写一次,不知道怎么写,望哥哥们帮下我

解决方案 »

  1.   

    沉了吧。
    这么复杂的东西用table更灵活。
      

  2.   

    gridview合并很麻烦的,还是换种思路吧
      

  3.   

    这个比较麻烦了 没办法把gridview当参数传递啊
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            this.GvRowMerge();
        }public void GvRowMerge(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowIndex == 0)
            {
                strText = e.Row.Cells[0].Text;
            }
            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != 0)
            {
                if (e.Row.Cells[0].Text == strText)
                {
                    intRowSpan++;
                    GridView1.Rows[intIndex].Cells[0].RowSpan = intRowSpan;
                    e.Row.Cells[0].Visible = false;
                }
                else
                {
                    intRowSpan = 1;
                    intIndex = e.Row.RowIndex;
                }
                strText = e.Row.Cells[0].Text;
            }}
      

  4.   

    那如果真的很复杂的话大家说用table怎么实现呢,教教我,我主要想从数据库绑定出来显示啊
      

  5.   


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
            this.GvRowMerge(sender,e); 
        }