合并单元格

 //合并内容相同的单元格
    private void SpanGrid()
    {
        int i, j, intSpan;  //i j 分别表示当前行,和下一行,intSpan要合并的列数
        string strTemp; //数据
        for (i = 0; i < dgvContentInfo.Rows.Count; i++)
        {
            intSpan = 1; //开始为1
            //'得到第一列第一行单元格中的内容
            strTemp = dgvContentInfo.Rows[i].Cells[0].Text;
            //循环判断。判断第一列中,和第一行相同的内容。相同做记号,intspan加一
            for (j = i + 1; j < dgvContentInfo.Rows.Count; j++)
            {
                if (string.Compare(strTemp, dgvContentInfo.Rows[j].Cells[0].Text) == 0)//表示相等
                {
                    intSpan += 1;
                    //利用datagrid的Itemspan属性
                    dgvContentInfo.Rows[i].Cells[0].RowSpan = intSpan;
                    dgvContentInfo.Rows[i].Cells[3].RowSpan = intSpan;
                    //把内容相同单元格隐藏
                    dgvContentInfo.Rows[j].Cells[0].Visible = false;
                    dgvContentInfo.Rows[j].Cells[3].Visible = false;
                }
                else
                {
                    break;
                }
            }
            i = j - 1;//退出了
        }
    }

原文地址:https://www.cnblogs.com/yzenet/p/2605084.html