DataGridView One of the merged cells (one column or row)

Source: Internet
Author: User

        #region "Test of merged cells (a column or row)"//int? is a search for a type (nullable type), a normal int cannot be NULL, and an int?, whose value can be null//private int?        NextRow = null; private int?        Nextcol = null; Call private void datagridview1_cellformatting after the Cellpainting method (object sender, System.Windows.Forms.DataGridViewCell Formattingeventargs e) {/*//column labeled if (e.columnindex >= 0 && This.datagr Idview1.columns[e.columnindex]. HeaderText = = "ParentID" && e.rowindex >= 0) {//If the same column cell value is the same as the next row, modify settings I F (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; }}} */*//Line marked if (e.columnindex >= 0 && This.datagridview1.columns[e.columnindex]. HeaderText = = "PHONE1" && e.rowindex >= 0) {//If the cell value is the same as the next column in the same row, modify the set 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; }}} */}//==========================//Draw cell private            void Datagridview1_cellpainting (object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e) { Merge vertically if (e.columnindex >= 0 && This.datagridview1.columns[e.columnindex]. HeaderText = = "ParentID" && e.rowindex >= 0) {using (Brush grid Brush = new SolidBrush (this.dataGridView1.GridColor), BackColorBrush = new SolidBrush (E.CELLSTYLE.BACKC                     Olor) {using (Pen Gridlinepen = new Pen (Gridbrush)) {   Erase original cell background e.graphics.fillrectangle (BackColorBrush, e.cellbounds); /****** draw cells to separate lines from each other, DataGridView themselves to handle the lines on the left and top edges, so simply draw the bottom and right border DataGridView controls to draw the cell, without drawing the left and top borders, a total With the right edge of the left cell, the bottom border of the previous cell *****///is not the last row and the cell value is not NULL if (E . RowIndex < this.datagridview1.rowcount-1 && This.datagridview1.rows[e.rowindex + 1]. Cells[e.columnindex]. Value = null) {//if different from the next cell value if (e.value.tos Tring ()! = This.datagridview1.rows[e.rowindex + 1]. Cells[e.columnindex]. Value.tostring ()) {//bottom edge of line E.gra Phics. DrawLine (Gridlinepen, E.cellbounds.left, E.cellbounds.bottom-1, E.cellbounds.right-1, E .                                CELLBOUNDS.BOTTOM-1);               Draw values                 if (e.value! = null) {E.graphics.draws                                        Tring (E.value.tostring (), E.cellstyle.font, Brushes.crimson, E.cellbounds.x + 2,                                E.cellbounds.y + 2, stringformat.genericdefault);                             }}//If the same as the next cell value else {//Background color//e.cellstyle.backcolor = Color.lightpin   K Only available in the Cellformatting method 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; Read-only (to avoid displaying values when you double-click a cell) This.datagridview1.rows[e.rowindex]. Cells[e.columnindex].                                ReadOnly = true; This.datagridview1.rows[e.rowindex + 1]. Cells[e.columnindex].                            ReadOnly = true;                            }}//The last row or cell value is null else { Bottom edge of line E.graphics.drawline (Gridlinepen, E.cellbounds.left, E.cellbou Nds.                                                         Bottom-1, E.cellbounds.right-1, e.cellbounds.bottom-1);                                Draw value 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);          }}////the Left line ()              E.graphics.drawline (Gridlinepen, E.cellbounds.left,//E.cellbounds.top, E.CELLB Ounds.                        Left,//e.cellbounds.bottom-1); Line E.graphics.drawline on the right (Gridlinepen, e.cellbounds.right-1, E.cellbo Unds.                        Top, E.cellbounds.right-1, e.cellbounds.bottom-1);                        Set processing event completion (key point), only set to Ture, to display the desired results.                    E.handled = true; }}}//Horizontal merge if (e.columnindex >= 0 && This.dataGridView1.Colum Ns[e.columnindex]. HeaderText = = "PHONE1" && e.rowindex >= 0) {using (Brush Gridbru SH = new SolidBrush (this.dataGridView1.GridColor), BackColorBrush = new SolidBrush (E.cellstyle.backcolo R) {using (Pen Gridlinepen = new Pen (gRidbrush) {//Erase original cell background e.graphics.fillrectangle (backcol                        Orbrush, E.cellbounds); /****** draw cells to separate lines from each other, DataGridView themselves to handle the lines on the left and top edges, so simply draw the bottom and right border DataGridView controls to draw the cell, without drawing the left and top borders, a total With the right border of the left cell, the bottom box of the previous cell *****///Not the last column and the cell value is not NULL if (E.columnindex < THIS.D Atagridview1.columncount-1 && This.datagridview1.rows[e.rowindex]. Cells[e.columnindex + 1]. Value = null) {if (e.value.tostring ()! = This.datagridview1.rows[e.row Index]. Cells[e.columnindex + 1]. Value.tostring ()) {//Line e.grap on right Hics.  DrawLine (Gridlinepen, E.cellbounds.right-1, E.cellbounds.top, E.cellbounds.right-1,                                E.CELLBOUNDS.BOTTOM-1);                    Draw values            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);                            }}//If the same as the next cell value else {//Background color//e.cellstyle.backcolor = Color.lightpink   ; Only available in the Cellformatting method 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; Read-only (lest the value be displayed when the cell is double-clicked) This.datagridview1.rows[e.rowindex]. Cells[E.columnindex].                                ReadOnly = true; This.datagridview1.rows[e.rowindex]. Cells[e.columnindex + 1].                            ReadOnly = true;                            }} else {//line to the right                                E.graphics.drawline (Gridlinepen, e.cellbounds.right-1, E.cellbounds.top,                                                         E.cellbounds.right-1, e.cellbounds.bottom-1); Draw value if (e.value! = null) {E.graphi Cs.                                    DrawString (e.value.tostring (), E.cellstyle.font, Brushes.crimson, E.cellbounds.x + 2,                            E.cellbounds.y + 2, stringformat.genericdefault); }}//bottom edge of line E.graphics.drawline (Gridlinepen, E.cellBounds.left, E.cellbounds.bottom-1, E.cellbounds.right-1, E.cellbou Nds.                        BOTTOM-1);                    E.handled = true; }}} #endregion

  

DataGridView One of the merged cells (one column or row)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.