something like this? is it possible?
colspan and row span on datagridview
can i colspan or row span a datagridview?
something like this? is it possible?


something like this? is it possible?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
WoernyPosted Jan 6, 2011, 3:08 PM
Amit ChoudharyPosted Mar 24, 2010, 2:51 AM
Try this code this is for merging the cells and rows.....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Drawing2D;
public class HMergedCell : DataGridViewTextBoxCell
{
private int m_nLeftColumn = 0;
private int m_nRightColumn = 0;
///
/// Column Index of the left-most cell to be merged.
/// This cell controls the merged text.
///
public int LeftColumn {
get { return m_nLeftColumn; }
set { m_nLeftColumn = value; }
}
///
/// Column Index of the right-most cell to be merged
///
public int RightColumn {
get { return m_nRightColumn; }
set { m_nRightColumn = value; }
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
try {
int mergeindex = ColumnIndex - m_nLeftColumn;
int i = 0;
int nWidth = 0;
int nWidthLeft = 0;
string strText = null;
Pen pen = new Pen(Brushes.Black);
// Draw the background
graphics.FillRectangle(new SolidBrush(SystemColors.Control), cellBounds);
// Draw the separator for rows
graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);
// Draw the right vertical line for the cell
if (ColumnIndex == m_nRightColumn) {
graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom);
}
// Draw the text
RectangleF rectDest = RectangleF.Empty;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
// Determine the total width of the merged cell
nWidth = 0;
for (i = m_nLeftColumn; i <= m_nRightColumn; i++) {
nWidth += this.OwningRow.Cells(i).Size.Width;
}
// Determine the width before the current cell.
nWidthLeft = 0;
for (i = m_nLeftColumn; i <= ColumnIndex - 1; i++) {
nWidthLeft += this.OwningRow.Cells(i).Size.Width;
}
// Retrieve the text to be displayed
strText = this.OwningRow.Cells(m_nLeftColumn).Value.ToString();
rectDest = new RectangleF(cellBounds.Left - nWidthLeft, cellBounds.Top, nWidth, cellBounds.Height);
graphics.DrawString(strText, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
}
catch (Exception ex) {
Trace.WriteLine(ex.ToString());
}
}
}
Please Mark as answer if it helps.