Click here to Skip to main content
15,896,278 members
Home / Discussions / C#
   

C#

 
GeneralRe: DateTimePicker Pin
J$1-Dec-09 7:10
J$1-Dec-09 7:10 
GeneralRe: DateTimePicker Pin
Saksida Bojan1-Dec-09 10:04
Saksida Bojan1-Dec-09 10:04 
QuestionHow to merge and split cells in datagridview? Pin
sususan30-Nov-09 19:20
sususan30-Nov-09 19:20 
AnswerRe: How to merge and split cells in datagridview? Pin
Abhijit Jana30-Nov-09 20:17
professionalAbhijit Jana30-Nov-09 20:17 
AnswerRe: How to merge and split cells in datagridview? Pin
souidi abderrahman30-Nov-09 20:56
souidi abderrahman30-Nov-09 20:56 
GeneralRe: How to merge and split cells in datagridview? Pin
dan!sh 30-Nov-09 21:24
professional dan!sh 30-Nov-09 21:24 
AnswerRe: How to merge and split cells in datagridview? Pin
dan!sh 30-Nov-09 21:27
professional dan!sh 30-Nov-09 21:27 
GeneralRe: How to merge and split cells in datagridview? Pin
Søren Fog31-Aug-10 3:02
Søren Fog31-Aug-10 3:02 
I got this working using a simple little (and ugly) hack Laugh | :laugh:

In the code below you will need to substitute (or implement) the three methods:
bool InsideMergedRowOfCells(e.ColumnIndex, e.RowIndex)
bool IsFirstOfMergedRowOfCells(e.ColumnIndex, e.RowIndex)
bool IsLastOfMergedRowOfCells(e.ColumnIndex, e.RowIndex)

They should be self explanatory.

The entire code snippet is taken from my handler of the CellPainting event for the grid (DataGridView). It depends utterly on a couple of things (and is therefore what I'd call a nasty hack): 1. It relies on the cells being painted in a set order, i.e. row by row from top to bottom, starting with the top-leftmost cell, and 2. It depends on the DataGridViewCellPaintingEventArgs's private "cellBounds" field to have that exact name. It's a read only (normally) but I am accessing and setting it via reflection.

This works, for now at least, but you need to handle some invalidating of the other cells (those not being repainted). It can be refined and improved, and I'll leave that up to the reader.

Anyway, here's the code

private Rectangle accumulatedMerge = Rectangle.Empty;
private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
	// Default painting all other places than the merged cells
	if (!InsideMergedRowOfCells(e.ColumnIndex, e.RowIndex)) return;

	// Rows come x-first, then y. Utterly dependant on this we sample the
	// first cell in a set of merged cells and patiently wait for the last in the range
	if (IsFirstOfMergedRowOfCells(e.ColumnIndex, e.RowIndex))
	{
		accumulatedMerge = e.CellBounds;	// Save the first rectangle
		e.Handled = true;	// Tell caller not to call the cell's paint method override
		return;				// Return, i.e. wait for the last merged cell in the row
	}

	// If we are asked to paint the last of a horizontal range of merged cells, the we need to do some work
	if (IsLastOfMergedRowOfCells(e.ColumnIndex, e.RowIndex))
	{
		// First, calculate the entire rectangle of the merged cells
		accumulatedMerge.Width = (e.CellBounds.X + e.CellBounds.Width) - _accumulatedMerge.X;

		// Dirty hack to inject the CellBounds rectangle (which is read-only), by accessing the field directly through reflection
		FieldInfo cellBoundsField = e.GetType().GetField("cellBounds", BindingFlags.NonPublic | BindingFlags.Instance);
		cellBoundsField.SetValue(e, accumulatedMerge);

		// Call the standard paint with the hacked rectangle injected
		e.Paint(accumulatedMerge, DataGridViewPaintParts.All);
	}

	// Tell caller not to call the cell's paint method override
	e.Handled = true;
}


Cheers Cool | :cool:
Søren L. Fog
http://sugarsoft.com

GeneralRe: How to merge and split cells in datagridview? Pin
Abhinav S18-Oct-10 18:25
Abhinav S18-Oct-10 18:25 
QuestionError in save file ... Pin
mostafa_h30-Nov-09 19:01
mostafa_h30-Nov-09 19:01 
AnswerRe: Error in save file ... Pin
souidi abderrahman30-Nov-09 21:01
souidi abderrahman30-Nov-09 21:01 
GeneralRe: Error in save file ... Pin
mostafa_h30-Nov-09 21:17
mostafa_h30-Nov-09 21:17 
AnswerRe: Error in save file ... Pin
CikaPero1-Dec-09 22:42
CikaPero1-Dec-09 22:42 
Questionfilling a dataGridView, editing and overwriting [modified] Pin
Emmet_Brown30-Nov-09 18:22
Emmet_Brown30-Nov-09 18:22 
AnswerRe: filling a dataGridView, editing and overwriting Pin
dan!sh 30-Nov-09 19:20
professional dan!sh 30-Nov-09 19:20 
GeneralRe: filling a dataGridView, editing and overwriting Pin
Emmet_Brown30-Nov-09 19:35
Emmet_Brown30-Nov-09 19:35 
GeneralRe: filling a dataGridView, editing and overwriting Pin
dan!sh 30-Nov-09 21:20
professional dan!sh 30-Nov-09 21:20 
GeneralRe: filling a dataGridView, editing and overwriting Pin
Emmet_Brown30-Nov-09 21:29
Emmet_Brown30-Nov-09 21:29 
AnswerRe: filling a dataGridView, editing and overwriting Pin
souidi abderrahman30-Nov-09 20:59
souidi abderrahman30-Nov-09 20:59 
GeneralRe: filling a dataGridView, editing and overwriting Pin
Emmet_Brown30-Nov-09 21:08
Emmet_Brown30-Nov-09 21:08 
AnswerRe: filling a dataGridView, editing and overwriting Pin
souidi abderrahman30-Nov-09 21:37
souidi abderrahman30-Nov-09 21:37 
GeneralRe: filling a dataGridView, editing and overwriting Pin
Emmet_Brown30-Nov-09 21:42
Emmet_Brown30-Nov-09 21:42 
QuestionCreate playlist and play using windows media player control Pin
yesu prakash30-Nov-09 18:03
yesu prakash30-Nov-09 18:03 
AnswerRe: Create playlist and play using windows media player control Pin
dan!sh 30-Nov-09 19:29
professional dan!sh 30-Nov-09 19:29 
QuestionObject array problem Pin
CrazyCoder2630-Nov-09 17:53
CrazyCoder2630-Nov-09 17:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.