Hi, Shobha
Yes. It's possible to do that, for DataGridView, you can handle the CellPainting event to paint the cells with the effect you need, something like this:
Code Snippet
partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
dt.Columns.Add("D");
for (int j = 0; j < 20; j++)
{
dt.Rows.Add("aa", "bb", "cc", "dd");
}
this.dataGridView1.DataSource = dt;
//this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
this.dataGridView1.CellPainting +=
new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//merge the cell[1,1] and cell[2,1]
if (e.RowIndex == 1)
{
if (e.ColumnIndex == 1)
{
e.PaintBackground(e.ClipBounds,true);
Rectangle r = e.CellBounds;
Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(2, 1, true);
r.Width += r1.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.DrawString("cell merged", e.CellStyle.Font, brFr, r,sf);
}
e.Handled = true;
}
if (e.ColumnIndex == 2)
{
using (Pen p = new Pen(this.dataGridView1.GridColor))
{
e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right, e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
e.Handled = true;
}
}
}
void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
//only redraw the cell[1,1] and cell[2,1] when scrolling
this.dataGridView1.InvalidateCell(1, 1);
this.dataGridView1.InvalidateCell(2, 1);
}
}
Best Regards, Zhi-xin Ye.
|