• 车辆信息统计报表管理系统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

  • 相关阅读:
    YTU 2802: 判断字符串是否为回文
    YTU 2392: 求各位数字之和
    YTU 2391: 求素数
    HDU 2064:汉诺塔III
    HDU 2187:悼念512汶川大地震遇难同胞——老人是真饿了
    HDU 2036:改革春风吹满地
    HDU 1081:To The Max
    HDU 1257:最少拦截系统
    YTU 2425: C语言习题 输出月份
    YTU 2420: C语言习题 不等长字符串排序
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136359.html
Copyright © 2020-2023  润新知