Click here to Skip to main content
11,704,107 members (69,176 online)
Click here to Skip to main content

DataGridVewTextBoxCell with Span Behaviour

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

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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
SuggestionCreate new column dynamically and center text Pin
cyrilcp18-Aug-15 4:52
membercyrilcp18-Aug-15 4:52 
QuestionProblem with borders when missing column headers and row headers but having frozen rows and columns Pin
Veselin Tenev9-Jun-15 5:38
memberVeselin Tenev9-Jun-15 5:38 
QuestionThanks Pin
Member 1143671712-Feb-15 22:05
memberMember 1143671712-Feb-15 22:05 
Questiontext alignment Pin
sudipto107-Dec-14 23:22
membersudipto107-Dec-14 23:22 
QuestionUnable to cast. Pin
Manjun.ravindra4-Jun-14 4:09
memberManjun.ravindra4-Jun-14 4:09 
AnswerRe: Unable to cast. Pin
vsvlad22-Apr-15 7:27
membervsvlad22-Apr-15 7:27 
QuestionCan't convert "System.Windows.Forms.DataGridViewTextBoxCell" to "SpannedDataGridView.DataGridViewTextBoxCellEx" Pin
BRS1530-Apr-14 2:38
memberBRS1530-Apr-14 2:38 
AnswerRe: Can't convert "System.Windows.Forms.DataGridViewTextBoxCell" to "SpannedDataGridView.DataGridViewTextBoxCellEx" Pin
vsvlad22-Apr-15 7:27
membervsvlad22-Apr-15 7:27 
QuestionThanks it really help me alot Pin
Ashish Bachhav12-Mar-14 7:00
memberAshish Bachhav12-Mar-14 7:00 
AnswerRe: Thanks it really help me alot Pin
Sergey Semyonov12-Mar-14 21:06
memberSergey Semyonov12-Mar-14 21:06 
QuestionAdd Borders to a cell Pin
Member 102053812-Feb-14 12:41
memberMember 102053812-Feb-14 12:41 
AnswerRe: Add Borders to a cell Pin
Sergey Semyonov13-Feb-14 2:24
memberSergey Semyonov13-Feb-14 2:24 
GeneralRe: Add Borders to a cell Pin
Member 102053814-Feb-14 9:56
memberMember 102053814-Feb-14 9:56 
GeneralRe: Add Borders to a cell Pin
Sergey Semyonov25-Feb-14 7:57
memberSergey Semyonov25-Feb-14 7:57 
QuestionNice post Pin
Tridip Bhattacharjee12-Dec-13 21:48
memberTridip Bhattacharjee12-Dec-13 21:48 
QuestionError with DataGridViewTextBoxCellEx Pin
Member 1045912611-Dec-13 1:48
memberMember 1045912611-Dec-13 1:48 
AnswerRe: Error with DataGridViewTextBoxCellEx Pin
Sergey Semyonov12-Dec-13 20:10
memberSergey Semyonov12-Dec-13 20:10 
QuestionIs it possible to do the same for the WPF control? Pin
Eli29-Oct-13 4:35
memberEli29-Oct-13 4:35 
QuestionIt is very useful Pin
Mahesh Kavali21-Sep-13 8:12
memberMahesh Kavali21-Sep-13 8:12 
QuestionError unable to emit Pin
irisangcm30-Aug-13 3:38
memberirisangcm30-Aug-13 3:38 
AnswerRe: Error unable to emit Pin
Sergey Semyonov1-Sep-13 9:06
memberSergey Semyonov1-Sep-13 9:06 
GeneralRe: Error unable to emit Pin
irisangcm3-Sep-13 9:58
memberirisangcm3-Sep-13 9:58 
Questiondatagridview.singleverticalborderadded() error Pin
song201331-Jul-13 22:13
membersong201331-Jul-13 22:13 
AnswerRe: datagridview.singleverticalborderadded() error Pin
Sergey Semyonov1-Aug-13 7:07
memberSergey Semyonov1-Aug-13 7:07 
GeneralRe: datagridview.singleverticalborderadded() error Pin
song20131-Aug-13 17:03
membersong20131-Aug-13 17:03 
GeneralRe: datagridview.singleverticalborderadded() error Pin
Sergey Semyonov1-Aug-13 20:55
memberSergey Semyonov1-Aug-13 20:55 
GeneralExcellent work! Pin
JulienV23-Jul-13 21:07
memberJulienV23-Jul-13 21:07 
Question.net 2.0 version Pin
Cool Smith4-May-13 12:09
memberCool Smith4-May-13 12:09 
AnswerRe: .net 2.0 version Pin
Sergey Semyonov5-Jun-13 8:23
memberSergey Semyonov5-Jun-13 8:23 
GeneralRe: .net 2.0 version Pin
Cool Smith5-Jun-13 10:30
memberCool Smith5-Jun-13 10:30 
AnswerRe: .net 2.0 version Pin
Sergey Semyonov18-Jun-13 21:11
memberSergey Semyonov18-Jun-13 21:11 
Questiondouble buffer for the datagridview Pin
tanghuawei21-Jan-13 17:28
membertanghuawei21-Jan-13 17:28 
AnswerRe: double buffer for the datagridview Pin
tipsybroom5-Jun-13 4:17
membertipsybroom5-Jun-13 4:17 
GeneralRe: double buffer for the datagridview Pin
Sergey Semyonov5-Jun-13 8:17
memberSergey Semyonov5-Jun-13 8:17 
GeneralRe: double buffer for the datagridview Pin
tanghuawei14-Jul-13 17:30
membertanghuawei14-Jul-13 17:30 
GeneralRe: double buffer for the datagridview Pin
tipsybroom3-Sep-13 2:38
membertipsybroom3-Sep-13 2:38 
GeneralRe: double buffer for the datagridview Pin
Member 1052976522-Apr-14 23:57
professionalMember 1052976522-Apr-14 23:57 
QuestionVBNet 2010 Pin
beto.silva.araujo@gmail.com30-Oct-12 13:08
memberbeto.silva.araujo@gmail.com30-Oct-12 13:08 
AnswerRe: VBNet 2010 Pin
Sergey Semyonov31-Oct-12 2:42
memberSergey Semyonov31-Oct-12 2:42 
GeneralRe: VBNet 2010 Pin
beto.silva.araujo@gmail.com31-Oct-12 7:20
memberbeto.silva.araujo@gmail.com31-Oct-12 7:20 
AnswerRe: VBNet 2010 Pin
H Lugo1-Jan-14 8:26
memberH Lugo1-Jan-14 8:26 
QuestionDataGridViewButtonCellEx ? Pin
Stefan Gies18-Oct-12 8:25
memberStefan Gies18-Oct-12 8:25 
AnswerRe: DataGridViewButtonCellEx ? Pin
Sergey Semyonov18-Oct-12 9:33
memberSergey Semyonov18-Oct-12 9:33 
QuestionBinding Pin
Krysiu_2926-Sep-12 19:08
memberKrysiu_2926-Sep-12 19:08 
AnswerRe: Binding Pin
Sergey Semyonov18-Oct-12 9:42
memberSergey Semyonov18-Oct-12 9:42 
Questionso good Pin
Member 437754611-Sep-12 21:54
memberMember 437754611-Sep-12 21:54 
QuestionSpannedDataGridView casting to datagridview Pin
ajayarav5-Jun-12 20:58
memberajayarav5-Jun-12 20:58 
AnswerRe: SpannedDataGridView casting to datagridview Pin
Krysiu_2926-Sep-12 18:24
memberKrysiu_2926-Sep-12 18:24 
AnswerRe: SpannedDataGridView casting to datagridview Pin
Sergey Semyonov18-Oct-12 9:46
memberSergey Semyonov18-Oct-12 9:46 
NewsHow to make click on any span cell select span correctly & port to VB .NET Pin
drunlar11-Jun-12 7:46
memberdrunlar11-Jun-12 7:46 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 18 Jun 2013
Article Copyright 2009 by Sergey Semyonov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid