DataGridView合並單元格(一列或一行)


        #region"合並單元格的測試(一列或一行)"
        // int?是搜索一種類型(可空類型),普通的int不能為null,而用int?,其值可以為null 
        //private int? nextrow = null;
        //private int? nextcol = null;

        //在CellPainting方法后調用
        private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
        {
            /*
            //列標示
            if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PARENTID"  && e.RowIndex >= 0)
            {
                //若與下一行同列單元格值相同則修改設置
                if (e.RowIndex < this.dataGridView1.RowCount - 1 && this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null)
                {
                    if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
                    {
                        e.CellStyle.BackColor = Color.LightPink;
                        this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Style.BackColor = Color.LightPink;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;
                        //this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].ReadOnly = true;
                    }
                }
                
            }
            */

            /*
            //行標示
            if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PHONE1" && e.RowIndex >= 0)
            {
                //若與同行下一列單元格值相同則修改設置
                if (e.ColumnIndex != this.dataGridView1.ColumnCount - 1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value != null)
                {
                    if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
                    {
                        e.CellStyle.BackColor = Color.LightBlue;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Style.BackColor = Color.LightPink;
                        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;
                        //this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].ReadOnly = true;
                    }
                }
            }
             */
        }

        //==========================

        //繪制單元格
        private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            //縱向合並
            if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PARENTID"  && e.RowIndex >= 0)
            {
                using (
                    Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // 擦除原單元格背景
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        /****** 繪制單元格相互間隔的區分線條,datagridview自己會處理左側和上邊緣的線條,因此只需繪制下邊框和和右邊框
                         DataGridView控件繪制單元格時,不繪制左邊框和上邊框,共用左單元格的右邊框,上一單元格的下邊框*****/
                        
                        //不是最后一行且單元格的值不為null
                        if (e.RowIndex < this.dataGridView1.RowCount - 1 && this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null)
                        {
                            //若與下一單元格值不同
                            if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
                            {
                                //下邊緣的線
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                                //繪制值
                                if (e.Value != null)
                                {
                                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                        Brushes.Crimson, e.CellBounds.X + 2,
                                        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                }
                            }
                            //若與下一單元格值相同  
                            else 
                            {
                                //背景顏色
                                //e.CellStyle.BackColor = Color.LightPink;   //僅在CellFormatting方法中可用
                                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;
                                this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;
                                //只讀(以免雙擊單元格時顯示值)
                                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;
                                this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].ReadOnly = true;
                            }
                        }
                        //最后一行或單元格的值為null
                        else
                        {
                            //下邊緣的線
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                             
                            //繪制值
                            if (e.Value != null)
                            {
                                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                    Brushes.Crimson, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 2, StringFormat.GenericDefault);
                            }
                        }

                        ////左側的線()
                        //e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                        //    e.CellBounds.Top, e.CellBounds.Left,
                        //    e.CellBounds.Bottom - 1);

                        //右側的線
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                            e.CellBounds.Top, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);

                        //設置處理事件完成(關鍵點),只有設置為ture,才能顯示出想要的結果。
                        e.Handled = true;
                    }
                }
            }

            //橫向合並
            if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "PHONE1" && e.RowIndex >= 0)
            {
                using (
                    Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // 擦除原單元格背景
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        /****** 繪制單元格相互間隔的區分線條,datagridview自己會處理左側和上邊緣的線條,因此只需繪制下邊框和和右邊框
                         DataGridView控件繪制單元格時,不繪制左邊框和上邊框,共用左單元格的右邊框,上一單元格的下邊框*****/

                        //不是最后一列且單元格的值不為null
                        if (e.ColumnIndex < this.dataGridView1.ColumnCount - 1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value != null)
                        {
                            if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
                            {
                                //右側的線
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                                    e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                                //繪制值
                                if (e.Value != null)
                                {
                                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                        Brushes.Crimson, e.CellBounds.X + 2,
                                        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                }
                            }
                            //若與下一單元格值相同  
                            else
                            {
                                //背景顏色
                                //e.CellStyle.BackColor = Color.LightPink;   //僅在CellFormatting方法中可用
                                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.LightPink;
                                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Style.BackColor = Color.LightPink;
                                //只讀(以免雙擊單元格時顯示值)
                                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;
                                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].ReadOnly = true;
                            }
                        }
                        else
                        {
                            //右側的線
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                                e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                             
                            //繪制值
                            if (e.Value != null)
                            {
                                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                    Brushes.Crimson, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 2, StringFormat.GenericDefault);
                            }
                        }
                        //下邊緣的線
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                                    e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        e.Handled = true;
                    }
                }

            }

        }

        #endregion

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM