How to merge datagridview cells with same value in winform?
How to Merge DataGridView Cell in Winformsenter link description here
This is the answer, because of my own mistake for not noticing the parameter for column and row are actually passed from cellpainting and cellformatting. This is the perfect answer
bool IsTheSameCellValue(int column, int row)
{
DataGridViewCell cell1 = dataGridView1[column, row];
DataGridViewCell cell2 = dataGridView1[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
return cell1.Value.ToString() == cell2.Value.ToString();
}
Howto add a row with horizontally merged cells to DataGridView
Here is working code for horizontal merge of the first row (both for centering cell content and proper border painting), initial code was found on MSDN:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//mering all cells in a first row
if (e.RowIndex == 0)
{
if (e.ColumnIndex == 0)
{
e.PaintBackground(e.ClipBounds, true);
Rectangle r = e.CellBounds;
for (int i = 1; i < (sender as DataGridView).ColumnCount; i++)
r.Width += (sender as DataGridView).GetCellDisplayRectangle(i, 0, true).Width;
r.Width -= 1;
r.Height -= 1;
using (SolidBrush brBk = new SolidBrush(e.CellStyle.BackColor))
using (SolidBrush brFr = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.FillRectangle(brBk, r);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, brFr, r, sf);
}
e.Handled = true;
}
else
if (e.ColumnIndex > 0)
{
using (Pen p = new Pen((sender as DataGridView).GridColor))
{
//bottom line of a cell
e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
//right vertical line of a last cell in a row
if (e.ColumnIndex == (sender as DataGridView).ColumnCount - 1)
e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
e.Handled = true;
}
}
}
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
//force redraw first row when scrolling
for (int i = 0; i < (sender as DataGridView).ColumnCount; i++)
(sender as DataGridView).InvalidateCell(i, 0);
}
Related Topics
Keeping ASP.NET Session Open/Alive
How to Get the Unix Timestamp in C#
Why Doesn't .Net/C# Optimize for Tail-Call Recursion
Casting Interfaces for Deserialization in JSON.Net
How to Change Network Settings (Ip Address, Dns, Wins, Host Name) with Code in C#
C# - Faster Alternatives to Setpixel and Getpixel for Bitmaps for Windows Forms App
How to Let an Asmx File Output JSON
Resolving Instances with ASP.NET Core Di from Within Configureservices
How to Create a Simple Proxy in C#
Detect Windows Version in .Net
Deserialize JSON with Known and Unknown Fields
How to Determine the Owner of a Process in C#
How to Programmatically Generate Keypress Events in C#
Launching an Application (.Exe) from C#
Run Two Async Tasks in Parallel and Collect Results in .Net 4.5