DataGridView Merging Cells

Source: Internet
Author: User

Yesterday, a colleague asked me DataGridView the problem of cell merging, and at first I imagined that I would figure out the rectangle of two cells,
Then use e directly. Graphics.FillRectangle (BackColorBrush, rectangle) fills the background color from the new one, and then draws the displayed characters, of course this idea is not feasible.

Here is the method I wrote a cell merge, in fact, this method is not good, but it seems to be a combination of cells, in fact, the actual DataGridView column structure is not changed, and this redraw method is not editable, so I still recommend if you encounter the problem of merging cells, It's better not to use DataGridView.
If you do not know can add (source sharing group 81582487) to communicate together
Code section:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;

Namespace Windowsdemo
{
public partial class Datagridviewmergecell:form
{
Public Datagridviewmergecell ()
{
InitializeComponent ();
Init ();
}

private void Datagridviewmergecell_load (object sender, EventArgs e)
{
int rowIndex = view. Rows.Add ();
DataGridViewRow row = view. Rows[rowindex];
Row. cells["Value"]. Value = "BBB";
Row. cells["Name"]. Value = "AAA";
Row. cells["Price"]. Value = "BBB";
Row. cells["Num"]. Value = "AAA";
RowIndex = view. Rows.Add ();
row = view. Rows[rowindex];
Row. cells["Value"]. Value = "BBB";
Row. cells["Name"]. Value = "AAA";
Row. cells["Price"]. Value = "BBB";
Row. cells["Num"]. Value = "AAA";
RowIndex = view. Rows.Add ();
row = view. Rows[rowindex];
Row. cells["Value"]. Value = "BBB";
Row. cells["Name"]. Value = "AAA";
Row. cells["Price"]. Value = "BBB";
Row. cells["Num"]. Value = "AAA";
RowIndex = view. Rows.Add ();
row = view. Rows[rowindex];
Row. cells["Value"]. Value = "BBB";
Row. cells["Name"]. Value = "AAA";
Row. cells["Price"]. Value = "BBB";
Row. cells["Num"]. Value = "AAA";
RowIndex = view. Rows.Add ();
row = view. Rows[rowindex];
Row. cells["Value"]. Value = "BBB";
Row. cells["Name"]. Value = "AAA";
Row. cells["Price"]. Value = "BBB";
Row. cells["Num"]. Value = "AAA";


}

private void Init () {

View. Allowusertoaddrows = false;
View. Columnheadersvisible = false;
View. AutoGenerateColumns = false;
View. AutoSize = false;
View. Rowheadersvisible = false;
View. Gridcolor = System.Drawing.ColorTranslator.FromHtml ("#F8F8FF");
View. Columnheadersheight = 60;

View. autoSizeColumnsMode = Datagridviewautosizecolumnsmode.none;
View. Columnheadersborderstyle = Datagridviewheaderborderstyle.single;
View. Columns.Add (New Datagridviewtextboxcolumn () {name = "name", HeaderText = "Body", Width = all, ReadOnly = True,visible = True , AutoSizeMode = Datagridviewautosizecolumnmode.none});
View. Columns.Add (New Datagridviewtextboxcolumn () {Name = "value", HeaderText = "", Width = new, ReadOnly = True, Visible = True , AutoSizeMode = Datagridviewautosizecolumnmode.none});
View. Columns.Add (New Datagridviewtextboxcolumn () {Name = "price", HeaderText = "Body", Width =, ReadOnly = true, Visible = tr UE, AutoSizeMode = Datagridviewautosizecolumnmode.none});
View. Columns.Add (New Datagridviewtextboxcolumn () {Name = "num", HeaderText = "", Width = new, ReadOnly = True, Visible = True, AutoSizeMode = Datagridviewautosizecolumnmode.fill});
View. CellPainting-= new Datagridviewcellpaintingeventhandler (view_cellpainting);
View. CellPainting + = new Datagridviewcellpaintingeventhandler (view_cellpainting);
}


void View_cellpainting (object sender, DataGridViewCellPaintingEventArgs e)
{
if (E.rowindex = = 1 && e.columnindex = = 1)
{
Brush BackColorBrush = new SolidBrush (E.cellstyle.backcolor);
E.paint (E.cellbounds, Datagridviewpaintparts.all);
E.graphics.fillrectangle (BackColorBrush, e.cellbounds);
Draw the background color (selected)
if (e.state = = (Datagridviewelementstates.displayed | datagridviewelementstates.selected | datagridviewelementstates.visible))
E.paintbackground (E.cellbounds, false);
Draw the original text separately and now change the color of the text
Brush fontcolor = new SolidBrush (E.cellstyle.forecolor);
E.graphics.drawstring ("", this. Font, FontColor, E.cellbounds, Stringformat.genericdefault);
Draw the bottom border line
Brush Gridbrush = new SolidBrush (This.view.GridColor);
Pen pen = new Pen (gridbrush);
E.graphics.drawline (pen, E.cellbounds.left, E.cellbounds.bottom-1,
E.cellbounds.right-1, e.cellbounds.bottom-1);
DataGridViewCell cell = This.view.rows[e.rowindex]. Cells[e.columnindex];

Cell. Value = "";
E.handled = true;
}
if (E.rowindex = = 1 && e.columnindex = = 2)
{
Brush BackColorBrush = new SolidBrush (E.cellstyle.backcolor);
E.paint (E.cellbounds, Datagridviewpaintparts.all);
E.graphics.fillrectangle (BackColorBrush, e.cellbounds);
Draw the background color (selected)
if (e.state = = (Datagridviewelementstates.displayed | datagridviewelementstates.selected | datagridviewelementstates.visible))
E.paintbackground (E.cellbounds, true);
Draw the original text separately and now change the color of the text
Brush fontcolor = new SolidBrush (E.cellstyle.forecolor);
E.graphics.drawstring ("", this. Font, FontColor, E.cellbounds, Stringformat.genericdefault);
Draw the bottom border line
Brush Gridbrush = new SolidBrush (This.view.GridColor);
Pen pen = new Pen (gridbrush);

E.graphics.drawline (pen, E.cellbounds.left, E.cellbounds.bottom-1,
E.cellbounds.right-1, e.cellbounds.bottom-1);

Draw right Border line
E.graphics.drawline (pen, e.cellbounds.right-1,
E.cellbounds.top, E.cellbounds.right-1,
E.CELLBOUNDS.BOTTOM-1);
DataGridViewCell cell = This.view.rows[e.rowindex]. Cells[e.columnindex];
Cell. Value = "";
Cell. Tag = "CCCCC";
Rectangle Rectanle = e.cellbounds;
Rectanle. X = Rectanle. X-15;
Rectanle. Y = Rectanle. Y + 5;
It's important to note that I'm not using e.graphics cause this is the only one that can be drawn in the current cell
And I was building a drawing graphics object on top of the DataGridView so that I could look at two cells and draw
Here, you can try it yourself.
Graphics graphics = This.view.CreateGraphics ();
Draw the original text separately and now change the color of the text
Graphics. DrawString ("CCCC", this. Font, New SolidBrush (E.cellstyle.forecolor), Rectanle, Stringformat.genericdefault);
E.handled = true;
}
}
}
}


The following is my proposal, although this seems to solve the problem, in fact, there are many shortcomings, one is not editable, and this also has a bug is to click on the left side of the border will be left to hide the text, I tried a lot of methods are not resolved, this should be the background color to cover the editing.

I recommend a third-party control that can merge cells (Devexpress.xtragrid.v7.3.dll), which I do not study now, but I will study it later, I will write an article

DataGridView Merging Cells

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.