DataGridView merged cells (multi-row multi-column merge)

Source: Internet
Author: User

First, click on the arrow at the top right of the display control (treelist) that you dragged in, select the data source in the Treelist task, add the project data source, select the database, the dataset, create a new connection, browse to select the database (*.mdb), click Next, then select "Table" to complete.

Second, the specific code is as follows:

        #region All column header names merged with merged cells (multiple rows and columns)//required (rows, columns) list<string> colsheadertext_v = new list<string>        ();        list<string> colsheadertext_h = new list<string> ();            private void Initformatcolumns () {Colsheadertext_v.add ("PHONE1");            Colsheadertext_v.add ("PHONE2");            Colsheadertext_h.add ("ImageIndex");            Colsheadertext_h.add ("ParentID");            Colsheadertext_h.add ("DEPARTMENT");        Colsheadertext_h.add ("location"); }//Draw cell private void datagridview1_cellpainting (object sender, System.Windows.Forms.DataGridViewCellPainti                 Ngeventargs e) {foreach (string fieldheadertext in Colsheadertext_h) {//vertically merged if (e.columnindex >= 0 && This.datagridview1.columns[e.columnindex].                        HeaderText = = Fieldheadertext && e.rowindex >= 0) {using ( Brush GridBrush = new SolidBrush (this.dataGridView1.GridColor), BackColorBrush = new SolidBrush (e.cellstyle.b                        Ackcolor)) {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 when drawing cells, not drawing the left border and the upper Border, the right border of the left cell is shared, the bottom frame of the previous cell *****///is not the last row, and the cell value is not NULL if (E.rowindex &L T 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.tostring ()! = This.datagridview1.rows[e.rowindex + 1]. Cells[e.columnindex].                              Value.tostring ())  {//bottom edge of line E.graphics.drawline (Gridlinepen, E.CELLB Ounds.                                    Left, E.cellbounds.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);                                }}//If the same as the next cell value else {//Background color//E.CELLSTYLE.BAC   Kcolor = Color.lightpink;                    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 (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 + 1]. Cells[e.columnindex].                                ReadOnly = true;                            }}//The last row or cell value is null else {//bottom edge of line E.graphics.drawline (Gridlinepen, E.ce Llbounds.left, E.cellbounds.bottom-1, E.cellbounds.right-1, e.cellbounds.bottom-1                                ); Draw value if (e.value! = nulL) {e.graphics.drawstring (e.value.tostring (), E.cellsty Le. Font, Brushes.crimson, E.cellbounds.x + 2, E .                                Cellbounds.y + 2, stringformat.genericdefault); }}////the Left line ()//e.graphics.drawline (grid                            Linepen, E.cellbounds.left,//E.cellbounds.top, E.cellbounds.left,                            E.CELLBOUNDS.BOTTOM-1);                                Line E.graphics.drawline on the right (Gridlinepen, e.cellbounds.right-1,                            E.cellbounds.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;            }        }}} foreach (String Fieldheadertext in Colsheadertext_v) { Horizontal merge if (e.columnindex >= 0 && This.datagridview1.columns[e.columnindex].                        HeaderText = = Fieldheadertext && e.rowindex >= 0) {using ( Brush Gridbrush = new SolidBrush (this.dataGridView1.GridColor), BackColorBrush = new Solidbru                        SH (e.cellstyle.backcolor)) {using (Pen Gridlinepen = new Pen (gridbrush)) {//Erase original cell background e.graphics.fillrectangle (Backcolorbru                            SH, 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 when drawing cells, not drawing the left border and the upper Border, the right border of the left cell is shared, the bottom box of the previous cell *****///is not the last column and the cell value is not 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.Ro Ws[e.rowindex]. Cells[e.columnindex + 1].                                    Value.tostring ()) {//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.graphics.drawstring (E.value.tostring (), E.cellstyle.font, Bru Shes.                 Crimson, E.cellbounds.x + 2, E.cellbounds.y + 2, stringformat.genericdefault);                   }}//If the value is the same as the next cell                                    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 E.graphics.drawline on the right (Gridlinepen, e.cellbounds.right-1, E.cellbounds.top,                                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); }}//bottom edge of line E.GRAPHICS.D Rawline (Gridlinepen, E.cellbounds.left, E.cellbounds.bottom-1, E.                            Cellbounds.right-1, e.cellbounds.bottom-1);                        E.handled = true;     }                    }                }       }} #endregion 

  

DataGridView merged cells (multi-row multi-column merge)

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.