Click here to Skip to main content
13,860,694 members
Click here to Skip to main content
Add your own
alternative version


97 bookmarked
Posted 10 Mar 2009
Licenced CPOL

DataGridVewTextBoxCell with Span Behaviour

, 18 Jun 2013
Rate this:
Please Sign up or sign in to vote.
DataGridViewTextBoxCell derived cell that can be merged with cells of the same type


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.

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.


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


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


About the Author

Sergey Semyonov
Software Developer EKRA
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

Questionproblem allowusertoordercolumns Pin
maxim^31-Oct-18 5:04
membermaxim^31-Oct-18 5:04 
QuestionDrawToBitmap not working for merged cells content Pin
LarnRegis28-Jun-18 4:01
memberLarnRegis28-Jun-18 4:01 
SuggestionSugestion for the error "System.Windows.Forms.DataGridViewTextBoxCell" to "SpannedDataGridView.DataGridViewTextBoxCellEx" Pin
Marcelo Fujii23-May-17 10:13
memberMarcelo Fujii23-May-17 10:13 
QuestionDelete dynamically some column Pin
Member 1283496128-Feb-17 2:36
memberMember 1283496128-Feb-17 2:36 
QuestionGreat work Pin
Member 88272063-Oct-16 2:16
memberMember 88272063-Oct-16 2:16 
PraiseYou saved my life. Thank you very much! Pin
Corwyn25-May-16 23:30
professionalCorwyn25-May-16 23:30 
GeneralRe: You saved my life. Thank you very much! Pin
Sergey Semyonov26-May-16 0:43
memberSergey Semyonov26-May-16 0:43 
SuggestionCreate new column dynamically and center text Pin
cyrilcp18-Aug-15 5:52
membercyrilcp18-Aug-15 5:52 
QuestionProblem with borders when missing column headers and row headers but having frozen rows and columns Pin
Veselin Tenev9-Jun-15 6:38
memberVeselin Tenev9-Jun-15 6:38 
QuestionThanks Pin
Member 1143671712-Feb-15 23:05
memberMember 1143671712-Feb-15 23:05 
Questiontext alignment Pin
sudipto108-Dec-14 0:22
membersudipto108-Dec-14 0:22 
QuestionUnable to cast. Pin
Manjun.ravindra4-Jun-14 5:09
memberManjun.ravindra4-Jun-14 5:09 
AnswerRe: Unable to cast. Pin
vsvlad22-Apr-15 8:27
membervsvlad22-Apr-15 8:27 
QuestionCan't convert "System.Windows.Forms.DataGridViewTextBoxCell" to "SpannedDataGridView.DataGridViewTextBoxCellEx" Pin
BRS1530-Apr-14 3:38
memberBRS1530-Apr-14 3:38 
AnswerRe: Can't convert "System.Windows.Forms.DataGridViewTextBoxCell" to "SpannedDataGridView.DataGridViewTextBoxCellEx" Pin
vsvlad22-Apr-15 8:27
membervsvlad22-Apr-15 8:27 
QuestionThanks it really help me alot Pin
Ashish Bachhav12-Mar-14 8:00
memberAshish Bachhav12-Mar-14 8:00 
AnswerRe: Thanks it really help me alot Pin
Sergey Semyonov12-Mar-14 22:06
memberSergey Semyonov12-Mar-14 22:06 
QuestionAdd Borders to a cell Pin
Member 102053812-Feb-14 13:41
memberMember 102053812-Feb-14 13:41 
AnswerRe: Add Borders to a cell Pin
Sergey Semyonov13-Feb-14 3:24
memberSergey Semyonov13-Feb-14 3:24 
GeneralRe: Add Borders to a cell Pin
Member 102053814-Feb-14 10:56
memberMember 102053814-Feb-14 10:56 
GeneralRe: Add Borders to a cell Pin
Sergey Semyonov25-Feb-14 8:57
memberSergey Semyonov25-Feb-14 8:57 
QuestionNice post Pin
Tridip Bhattacharjee12-Dec-13 22:48
professionalTridip Bhattacharjee12-Dec-13 22:48 
QuestionError with DataGridViewTextBoxCellEx Pin
Member 1045912611-Dec-13 2:48
memberMember 1045912611-Dec-13 2:48 
AnswerRe: Error with DataGridViewTextBoxCellEx Pin
Sergey Semyonov12-Dec-13 21:10
memberSergey Semyonov12-Dec-13 21:10 
QuestionIs it possible to do the same for the WPF control? Pin
Eli29-Oct-13 5:35
memberEli29-Oct-13 5:35 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 18 Jun 2013
Article Copyright 2009 by Sergey Semyonov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid