Click here to Skip to main content
Click here to Skip to main content

DataGridVewTextBoxCell with Span Behaviour

By , 21 Dec 2010
 

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.

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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Question.net 2.0 versionmemberCool Smith4 May '13 - 12:09 
Questiondouble buffer for the datagridviewmembertanghuawei21 Jan '13 - 17:28 
QuestionVBNet 2010memberbeto.silva.araujo@gmail.com30 Oct '12 - 13:08 
AnswerRe: VBNet 2010memberSergey Semyonov31 Oct '12 - 2:42 
GeneralRe: VBNet 2010memberbeto.silva.araujo@gmail.com31 Oct '12 - 7:20 
QuestionDataGridViewButtonCellEx ?memberStefan Gies18 Oct '12 - 8:25 
AnswerRe: DataGridViewButtonCellEx ?memberSergey Semyonov18 Oct '12 - 9:33 
QuestionBindingmemberKrysiu_2926 Sep '12 - 19:08 
AnswerRe: BindingmemberSergey Semyonov18 Oct '12 - 9:42 
Questionso goodmemberMember 437754611 Sep '12 - 21:54 
QuestionSpannedDataGridView casting to datagridviewmemberajayarav5 Jun '12 - 20:58 
AnswerRe: SpannedDataGridView casting to datagridviewmemberKrysiu_2926 Sep '12 - 18:24 
AnswerRe: SpannedDataGridView casting to datagridviewmemberSergey Semyonov18 Oct '12 - 9:46 
NewsHow to make click on any span cell select span correctly & port to VB .NETmemberdrunlar11 Jun '12 - 7:46 
QuestionLooks like what I want to domemberLee Belcher22 Jan '12 - 14:17 
AnswerRe: Looks like what I want to domemberSergey Semyonov22 Jan '12 - 20:10 
GeneralRe: Looks like what I want to domembernimolZero23 May '12 - 23:27 
Questiondoublebufferingmemberxboxproject19 Apr '11 - 1:16 
AnswerRe: doublebufferingmemberSergey Semyonov25 Apr '11 - 0:32 
GeneralRe: doublebufferingmemberxboxproject26 Apr '11 - 1:08 
AnswerRe: doublebufferingmemberSergey Semyonov27 Apr '11 - 4:30 
Generallast question for this fabulous control :-)membergertdebusser15 Dec '10 - 23:53 
GeneralRe: last question for this fabulous control :-)memberSergey Semyonov16 Dec '10 - 3:18 
GeneralRe: last question for this fabulous control :-)memberSergey Semyonov21 Dec '10 - 2:08 
Generaldouble questionmembergertdebusser8 Nov '10 - 23:31 
GeneralRe: double questionmemberSergey Semyonov9 Nov '10 - 1:18 
GeneralRe: double questionmembergertdebusser9 Nov '10 - 5:19 
GeneralRe: double questionmemberSergey Semyonov11 Nov '10 - 20:26 
GeneralBug when frozen cellsmemberCLOPEZ8 Nov '10 - 17:52 
GeneralRe: Bug when frozen cellsmemberSergey Semyonov8 Nov '10 - 20:26 
GeneralRe: Bug when frozen cellsmemberCLOPEZ9 Nov '10 - 4:37 
GeneralRe: Bug when frozen cellsmemberSergey Semyonov11 Nov '10 - 20:27 
GeneralBug with RTL ...memberA7mad_13 Jul '10 - 13:08 
GeneralRe: Bug with RTL ...memberSergey Semyonov13 Jul '10 - 23:13 
GeneralRe: Bug with RTL ...memberSergey Semyonov15 Jul '10 - 21:29 
GeneralFirst Row/Column ProblemmemberMember 30382169 Mar '10 - 3:27 
GeneralRe: First Row/Column ProblemmemberSergey Semyonov10 Mar '10 - 6:03 
GeneralRe: First Row/Column ProblemmemberGennadiy, Erlangen11 Mar '10 - 1:34 
GeneralRe: First Row/Column ProblemmemberSergey Semyonov11 Mar '10 - 9:24 
GeneralRe: First Row/Column ProblemmemberGennadiy, Erlangen15 Mar '10 - 4:52 
GeneralRe: First Row/Column ProblemmemberSergey Semyonov15 Mar '10 - 7:07 
Generalcan't spaned when binding grid.memberdcll26 Dec '09 - 18:04 
GeneralRe: can't spaned when binding grid.memberSergey Semyonov27 Dec '09 - 8:04 
GeneralRe: can't spaned when binding grid.memberdcll27 Dec '09 - 13:54 
QuestionVB.NET versionmemberTigertechie114 Sep '09 - 7:09 
AnswerRe: VB.NET versionmemberSergey Semyonov14 Sep '09 - 23:57 
GeneralRe: VB.NET versionmemberTigertechie116 Sep '09 - 4:28 
GeneralPaint image in spanned cellmemberMike McGill24 Jun '09 - 11:00 
GeneralRe: Paint image in spanned cellmemberSergey Semyonov25 Jun '09 - 6:32 
GeneralRe: Paint image in spanned cellmemberMike McGill25 Jun '09 - 16:56 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 21 Dec 2010
Article Copyright 2009 by Sergey Semyonov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid