Click here to Skip to main content
15,881,139 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 276.8K   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: double buffer for the datagridview Pin
Sergey Semyonov5-Jun-13 8:17
Sergey Semyonov5-Jun-13 8:17 
GeneralRe: double buffer for the datagridview Pin
tanghuawei14-Jul-13 17:30
professionaltanghuawei14-Jul-13 17:30 
GeneralRe: double buffer for the datagridview Pin
tipsybroom3-Sep-13 2:38
tipsybroom3-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
beto.silva.araujo@gmail.com30-Oct-12 13:08 
AnswerRe: VBNet 2010 Pin
Sergey Semyonov31-Oct-12 2:42
Sergey Semyonov31-Oct-12 2:42 
GeneralRe: VBNet 2010 Pin
beto.silva.araujo@gmail.com31-Oct-12 7:20
beto.silva.araujo@gmail.com31-Oct-12 7:20 
AnswerRe: VBNet 2010 Pin
Heriberto Lugo1-Jan-14 8:26
Heriberto Lugo1-Jan-14 8:26 
That is because you have to cast it.
try this:

VB
Dim RowDtGrid As New DataGridViewRow
RowDtGrid = DtGrid.Rows(0)
CType(RowDtGrid.Cells("Coluna1"), SpannedDataGridView.DataGridViewTextBoxCellEx).ColumnSpan = 3

QuestionDataGridViewButtonCellEx ? Pin
Stefan Gies18-Oct-12 8:25
Stefan Gies18-Oct-12 8:25 
AnswerRe: DataGridViewButtonCellEx ? Pin
Sergey Semyonov18-Oct-12 9:33
Sergey Semyonov18-Oct-12 9:33 
QuestionBinding Pin
Krysiu_2926-Sep-12 19:08
Krysiu_2926-Sep-12 19:08 
AnswerRe: Binding Pin
Sergey Semyonov18-Oct-12 9:42
Sergey Semyonov18-Oct-12 9:42 
Questionso good Pin
Member 437754611-Sep-12 21:54
Member 437754611-Sep-12 21:54 
QuestionSpannedDataGridView casting to datagridview Pin
ajayarav5-Jun-12 20:58
ajayarav5-Jun-12 20:58 
AnswerRe: SpannedDataGridView casting to datagridview Pin
Krysiu_2926-Sep-12 18:24
Krysiu_2926-Sep-12 18:24 
AnswerRe: SpannedDataGridView casting to datagridview Pin
Sergey Semyonov18-Oct-12 9:46
Sergey 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
drunlar11-Jun-12 7:46 
QuestionLooks like what I want to do Pin
Lee Belcher22-Jan-12 14:17
Lee Belcher22-Jan-12 14:17 
AnswerRe: Looks like what I want to do Pin
Sergey Semyonov22-Jan-12 20:10
Sergey Semyonov22-Jan-12 20:10 
GeneralRe: Looks like what I want to do Pin
nimolZero23-May-12 23:27
nimolZero23-May-12 23:27 
Questiondoublebuffering Pin
xboxproject19-Apr-11 1:16
xboxproject19-Apr-11 1:16 
AnswerRe: doublebuffering Pin
Sergey Semyonov25-Apr-11 0:32
Sergey Semyonov25-Apr-11 0:32 
GeneralRe: doublebuffering Pin
xboxproject26-Apr-11 1:08
xboxproject26-Apr-11 1:08 
AnswerRe: doublebuffering Pin
Sergey Semyonov27-Apr-11 4:30
Sergey Semyonov27-Apr-11 4:30 
GeneralRe: doublebuffering Pin
tipsybroom5-Jun-13 4:21
tipsybroom5-Jun-13 4:21 

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.