zoukankan      html  css  js  c++  java
  • 车辆信息统计报表管理系统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

  • 相关阅读:
    课堂练习
    软件工程课堂练习二维数组子数组和最大值,只要连续就好
    结对项目电梯调度
    第二次课堂练习
    软件工程课堂练习二维数组子数组和的最大值
    敏捷开发方法
    软件工程个人作业
    电梯调度
    创意
    02合并frame
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136359.html
Copyright © 2011-2022 走看看