Click here to Skip to main content
Licence CPOL
First Posted 10 Mar 2009
Views 52,420
Downloads 1,930
Bookmarked 70 times

DataGridVewTextBoxCell with Span Behaviour

By Sergey Semyonov | 21 Dec 2010
DataGridViewTextBoxCell derived cell that can be merged with cells of the same type
1 vote, 5.6%
1
1 vote, 5.6%
2

3
3 votes, 16.7%
4
13 votes, 72.2%
5
4.72/5 - 18 votes
2 removed
μ 4.32, σa 2.06 [?]

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


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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionLooks like what I want to do PinmemberLee Belcher15:17 22 Jan '12  
AnswerRe: Looks like what I want to do PinmemberSergey Semyonov21:10 22 Jan '12  
Questiondoublebuffering Pinmemberxboxproject2:16 19 Apr '11  
AnswerRe: doublebuffering PinmemberSergey Semyonov1:32 25 Apr '11  
GeneralRe: doublebuffering Pinmemberxboxproject2:08 26 Apr '11  
AnswerRe: doublebuffering PinmemberSergey Semyonov5:30 27 Apr '11  
Generallast question for this fabulous control :-) Pinmembergertdebusser0:53 16 Dec '10  
GeneralRe: last question for this fabulous control :-) PinmemberSergey Semyonov4:18 16 Dec '10  
GeneralRe: last question for this fabulous control :-) PinmemberSergey Semyonov3:08 21 Dec '10  
Generaldouble question Pinmembergertdebusser0:31 9 Nov '10  
GeneralRe: double question PinmemberSergey Semyonov2:18 9 Nov '10  
GeneralRe: double question Pinmembergertdebusser6:19 9 Nov '10  
GeneralRe: double question PinmemberSergey Semyonov21:26 11 Nov '10  
GeneralBug when frozen cells PinmemberCLOPEZ18:52 8 Nov '10  
GeneralRe: Bug when frozen cells PinmemberSergey Semyonov21:26 8 Nov '10  
GeneralRe: Bug when frozen cells PinmemberCLOPEZ5:37 9 Nov '10  
GeneralRe: Bug when frozen cells PinmemberSergey Semyonov21:27 11 Nov '10  
GeneralBug with RTL ... PinmemberA7mad_14:08 13 Jul '10  
GeneralRe: Bug with RTL ... PinmemberSergey Semyonov0:13 14 Jul '10  
GeneralRe: Bug with RTL ... PinmemberSergey Semyonov22:29 15 Jul '10  
GeneralFirst Row/Column Problem PinmemberMember 30382164:27 9 Mar '10  
GeneralRe: First Row/Column Problem PinmemberSergey Semyonov7:03 10 Mar '10  
GeneralRe: First Row/Column Problem PinmemberGennadiy, Erlangen2:34 11 Mar '10  
GeneralRe: First Row/Column Problem PinmemberSergey Semyonov10:24 11 Mar '10  
GeneralRe: First Row/Column Problem PinmemberGennadiy, Erlangen5:52 15 Mar '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120209.1 | Last Updated 21 Dec 2010
Article Copyright 2009 by Sergey Semyonov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid