Click here to Skip to main content
15,885,875 members
Articles / Desktop Programming / Windows Forms

DataGridVewTextBoxCell with Span Behaviour

Rate me:
Please Sign up or sign in to vote.
4.92/5 (41 votes)
18 Jun 2013CPOL1 min read 277.4K   17.4K   102   114
DataGridViewTextBoxCell derived cell that can be merged with cells of the same type
Image 1

Introduction

The standard System.Windows.Forms.DataGridView cells do not support merging the normal way. At some forums, developers advise the use of the CellPainting event of DataGridView. But this solution is not good if you need to realize complicated mergings. DataGridViewTextBoxCellEx supports spanning without any additional code.

Using the Code

To use DataGridViewTextBoxCellEx in a project, include the DataGridViewTextBoxCellEx.cs file, and then you can add DataGridViewTextBoxColumnEx in the columns editor of the DataGridView control.

Merging of cells is performed by setting the ColumnSpan and RowSpan properties of the left top DataGridViewTextBoxCellEx cell of the target region. For successful merging, all cells of the region must be of the DataGridViewTextBoxCellEx type.

C#
var cell = (DataGridViewTextBoxCellEx)dataGridView1[0, 0];
cell.ColumnSpan = 3;
cell.RowSpan = 2;

As a result, the dataGridView1[0, 0] cell will occupy the region from (0, 0) to (2, 1) (see screenshot at the top). The merged cell will look as the top left cell of the region. And if you wish to address a merged cell, you need to address the top left cell of the merged region. The merged region will look and behave as a normal System.Windows.Forms.DataGridViewTextBoxCell. You can edit it the standard way (pressing F2, clicking or typing text).

DataGridViewTextBoxCellEx also has a property OwnerCell. For unmerged cells and for the left top cells of the merged regions, it is null. For other cells of the merged region, the property points to the left top cell.

History

  • 11th March, 2009: Initial post
  • 9th March, 2010: Article updated
  • 21st December, 2010: Demo and source files updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer EKRA
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Paint image in spanned cell Pin
Mike McGill6-Jul-09 7:41
Mike McGill6-Jul-09 7:41 
GeneralMy vote of 1 Pin
krisk773-Jun-09 10:44
krisk773-Jun-09 10:44 
GeneralVisual Studio 2005 Pin
ChristinaSeay20-Apr-09 2:56
ChristinaSeay20-Apr-09 2:56 
GeneralRe: Visual Studio 2005 Pin
Sergey Semyonov20-Apr-09 7:12
Sergey Semyonov20-Apr-09 7:12 
QuestionColumn Header Span of DataGridView Pin
jasim-code22-Mar-09 1:23
jasim-code22-Mar-09 1:23 
AnswerRe: Column Header Span of DataGridView Pin
Sergey Semyonov22-Mar-09 8:43
Sergey Semyonov22-Mar-09 8:43 
GeneralFound defects/bugs Pin
MuForum20-Mar-09 12:33
MuForum20-Mar-09 12:33 
GeneralRe: Found defects/bugs Pin
Sergey Semyonov24-Mar-09 5:55
Sergey Semyonov24-Mar-09 5:55 
GeneralRe: Found defects/bugs Pin
MuForum24-Mar-09 21:35
MuForum24-Mar-09 21:35 
GeneralRe: Found defects/bugs Pin
pingosk30-Jun-09 22:44
pingosk30-Jun-09 22:44 
GeneralPaint bug when Row.Frozen = true; Pin
Member 437224214-Mar-09 14:40
Member 437224214-Mar-09 14:40 
GeneralRe: Paint bug when Row.Frozen = true; Pin
Sergey Semyonov15-Mar-09 21:03
Sergey Semyonov15-Mar-09 21:03 
GeneralGood job, Sergey! Pin
Member 437224214-Mar-09 4:46
Member 437224214-Mar-09 4:46 
GeneralMy vote of 2 Pin
Itay Sagui10-Mar-09 23:22
Itay Sagui10-Mar-09 23:22 

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.