车辆信息统计报表管理系统--GridView和DataGridView合并行和列

  源代码:

        ///<summary>

        /// Web合并相同数据的行标题

        ///</summary>

        ///<param name="gv"></param>

        ///<param name="col"></param>

        ///<param name="colName"></param>

        public static void Unite(GridView gv, int[] col, string[] colName)

        {

            int i;

            string LastType1;//主键第一列字段名

            string LastType2;

            int LastCell;

            if (gv.Rows.Count > 0)

            {

                for (int j = 0; j < col.Length; j++)

                {

                    LastType1 = gv.Rows[0].Cells[int.Parse(colName[0].ToString())].Text;

                    LastType2 = gv.Rows[0].Cells[int.Parse(colName[j].ToString())].Text;

                    gv.Rows[0].Cells[col[j]].RowSpan = 1;

                    LastCell = 0;

                    for (i = 1; i < gv.Rows.Count; i++)

                    {

                        if (gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Text == LastType2 && gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Text == LastType1)

                        {

                            gv.Rows[i].Cells[col[j]].Visible = false;

                            gv.Rows[LastCell].Cells[col[j]].RowSpan++;

                        }

                        else

                        {

                            LastType1 = gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Text;

                            LastType2 = gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Text;

                            LastCell = i;

                            gv.Rows[i].Cells[col[j]].RowSpan = 1;

                        }

                    }

                }

            }

        }

 

        ///<summary>

        /// Web设置指定行合并列

        ///</summary>

        ///<param name="gv"></param>

        ///<param name="pRowIndex">要合并列的行号</param>

        public static void UniteColumn(GridView gv, int pRowIndex,int pColIndex,int pSpanCount)

        {

            gv.Rows[pRowIndex].Cells[pColIndex].ColumnSpan = pSpanCount;

          

            for (int i = 1; i < pSpanCount; i++)

            {

                gv.Rows[pRowIndex].Cells[pColIndex + i].Visible = false;

            }

            gv.Rows[pRowIndex].Cells[pColIndex].Width = Unit.Pixel(0);

           

        }

 

        ///<summary>

        /// Winform合并相同数据的行标题

        ///</summary>

        ///<param name="gv"></param>

        ///<param name="col"></param>

        ///<param name="colName"></param>

        public static void Unite(DataGridView gv, int[] col, string[] colName)

        {

            int i;

            string LastType1;//主键第一列字段名

            string LastType2;

            int LastCell;

            if (gv.Rows.Count > 0)

            {

                for (int j = 0; j < col.Length; j++)

                {

                    LastType1 = gv.Rows[0].Cells[int.Parse(colName[0].ToString())].Value.ToString();

                    LastType2 = gv.Rows[0].Cells[int.Parse(colName[j].ToString())].Value.ToString();

                    ((gv.Rows[0].Cells[col[j]]) as  DataGridViewTextBoxCellEx).RowSpan = 1;

                    LastCell = 0;

                    for (i = 1; i < gv.Rows.Count; i++)

                    {

                        if (gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Value.ToString() == LastType2 && gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Value.ToString() == LastType1)

                        {

                            //gv.Rows[i].Cells[col[j]].Visible = false;

                            (gv.Rows[LastCell].Cells[col[j]] as DataGridViewTextBoxCellEx).RowSpan++;

                        }

                        else

                        {

                            LastType1 = gv.Rows[i].Cells[int.Parse(colName[0].ToString())].Value.ToString();

                            LastType2 = gv.Rows[i].Cells[int.Parse(colName[j].ToString())].Value.ToString();

                            LastCell = i;

                             (gv.Rows[i].Cells[col[j]] as DataGridViewTextBoxCellEx).RowSpan = 1;

                        }

                    }

                }

            }

        }

      ///<summary>

        /// DataGridView设置指定行合并列

      ///</summary>

      ///<param name="pDataGridView"></param>

        ///<param name="pRowIndex">要合并列的行号</param>

        ///<param name="pColumnIndex">起始列索引数</param>

        ///<param name="pColumnNumber">合并列数量</param>

        ///<param name="pBackColor">背景色彩</param>

        public static void SetColumnMerge(DataGridView pDataGridView, int pRowIndex, int pColumnIndex, int pColumnNumber, Color pBackColor)

        {

            var cell = (DataGridViewTextBoxCellEx)pDataGridView[pColumnIndex, pRowIndex];

            cell.ColumnSpan = pColumnNumber;

            cell.RowSpan = 1;

            cell.Style.BackColor = pBackColor;

        }

 

调用:

                                                       UnitGVTitle.UniteColumn(gv, rowCount, 0, 3);

                            UnitGVTitle.UniteColumn(gv, rowCount, 3, 1);

                            UnitGVTitle.UniteColumn(gv, rowCount, 4, 3);

 

效果:

查看更多精彩图片

下载

DataGridViewTextBoxCellEx.cs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值