Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Merging Cells in Listview/Datagridview etc...?
 

Merging Cells in Listview/Datagridview etc...?

Hi,

I am working on windows application using c#, I want to know is it possible to merge cells in ListView or Datagridview or any other controls?

How can i achieve this?Its very urgent.

Please help me.

Regards,

Shobha

shobha aradhya  Friday, January 18, 2008 6:48 AM

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.

Zhi-Xin Ye  Wednesday, January 23, 2008 3:16 AM

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.

Zhi-Xin Ye  Wednesday, January 23, 2008 3:16 AM
Hi guys!!!

I recommend use this after the "for bucle":

dt.Columns["B"].ReadOnly = true;
dt.Columns["C"].ReadOnly = true;

To avoid edit cell.



r_i_m_e_r  Thursday, August 14, 2008 2:11 AM

You can use google to search for other answers

Custom Search

More Threads

• Keeping the Same MenuStrip
• Hi everyone
• Error:Attempt to read or write protected the memory.
• Trouble with <Null> values in Date Fields
• Disable Auto-update?
• list all forms in a c# project
• Programically selecting an item in DataGridComboBoxCell
• Change Windows Language
• Terrarium code - why am I not suprised
• TaskVision Client Install error