Click here to Skip to main content
11,411,922 members (60,522 online)
Click here to Skip to main content

DataGridVewTextBoxCell with Span Behaviour

, 18 Jun 2013 CPOL
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

Comments and Discussions

QuestionThanks PinmemberMember 1143671712-Feb-15 23:05 
Questiontext alignment Pinmembersudipto108-Dec-14 0:22 
QuestionUnable to cast. PinmemberManjun.ravindra4-Jun-14 5:09 
AnswerRe: Unable to cast. Pinmembervsvlad22-Apr-15 8:27 
QuestionCan't convert "System.Windows.Forms.DataGridViewTextBoxCell" to "SpannedDataGridView.DataGridViewTextBoxCellEx" PinmemberBRS1530-Apr-14 3:38 
AnswerRe: Can't convert "System.Windows.Forms.DataGridViewTextBoxCell" to "SpannedDataGridView.DataGridViewTextBoxCellEx" Pinmembervsvlad22-Apr-15 8:27 
QuestionThanks it really help me alot PinmemberAshish Bachhav12-Mar-14 8:00 
AnswerRe: Thanks it really help me alot PinmemberSergey Semyonov12-Mar-14 22:06 
QuestionAdd Borders to a cell PinmemberMember 102053812-Feb-14 13:41 
AnswerRe: Add Borders to a cell PinmemberSergey Semyonov13-Feb-14 3:24 
GeneralRe: Add Borders to a cell PinmemberMember 102053814-Feb-14 10:56 
GeneralRe: Add Borders to a cell PinmemberSergey Semyonov25-Feb-14 8:57 
QuestionNice post PinmemberTridip Bhattacharjee12-Dec-13 22:48 
QuestionError with DataGridViewTextBoxCellEx [modified] PinmemberMember 1045912611-Dec-13 2:48 
AnswerRe: Error with DataGridViewTextBoxCellEx PinmemberSergey Semyonov12-Dec-13 21:10 
QuestionIs it possible to do the same for the WPF control? PinmemberEli29-Oct-13 5:35 
QuestionIt is very useful PinmemberMahesh Kavali21-Sep-13 9:12 
QuestionError unable to emit [modified] Pinmemberirisangcm30-Aug-13 4:38 
AnswerRe: Error unable to emit PinmemberSergey Semyonov1-Sep-13 10:06 
GeneralRe: Error unable to emit Pinmemberirisangcm3-Sep-13 10:58 
Questiondatagridview.singleverticalborderadded() error Pinmembersong201331-Jul-13 23:13 
AnswerRe: datagridview.singleverticalborderadded() error PinmemberSergey Semyonov1-Aug-13 8:07 
GeneralRe: datagridview.singleverticalborderadded() error Pinmembersong20131-Aug-13 18:03 
GeneralRe: datagridview.singleverticalborderadded() error PinmemberSergey Semyonov1-Aug-13 21:55 
GeneralExcellent work! PinmemberJulienV23-Jul-13 22:07 2.0 version PinmemberCool Smith4-May-13 13:09 
AnswerRe: .net 2.0 version PinmemberSergey Semyonov5-Jun-13 9:23 
GeneralRe: .net 2.0 version PinmemberCool Smith5-Jun-13 11:30 
AnswerRe: .net 2.0 version PinmemberSergey Semyonov18-Jun-13 22:11 
Questiondouble buffer for the datagridview Pinmembertanghuawei21-Jan-13 18:28 
AnswerRe: double buffer for the datagridview Pinmembertipsybroom5-Jun-13 5:17 
GeneralRe: double buffer for the datagridview [modified] PinmemberSergey Semyonov5-Jun-13 9:17 
GeneralRe: double buffer for the datagridview Pinmembertanghuawei14-Jul-13 18:30 
GeneralRe: double buffer for the datagridview Pinmembertipsybroom3-Sep-13 3:38 
GeneralRe: double buffer for the datagridview PinprofessionalMember 1052976523-Apr-14 0:57 
QuestionVBNet 2010 Pinmemberbeto.silva.araujo@gmail.com30-Oct-12 14:08 
AnswerRe: VBNet 2010 PinmemberSergey Semyonov31-Oct-12 3:42 
GeneralRe: VBNet 2010 Pinmemberbeto.silva.araujo@gmail.com31-Oct-12 8:20 
AnswerRe: VBNet 2010 PinmemberH Lugo1-Jan-14 9:26 
QuestionDataGridViewButtonCellEx ? PinmemberStefan Gies18-Oct-12 9:25 
AnswerRe: DataGridViewButtonCellEx ? PinmemberSergey Semyonov18-Oct-12 10:33 
QuestionBinding PinmemberKrysiu_2926-Sep-12 20:08 
AnswerRe: Binding PinmemberSergey Semyonov18-Oct-12 10:42 
Questionso good PinmemberMember 437754611-Sep-12 22:54 
QuestionSpannedDataGridView casting to datagridview Pinmemberajayarav5-Jun-12 21:58 
AnswerRe: SpannedDataGridView casting to datagridview PinmemberKrysiu_2926-Sep-12 19:24 
AnswerRe: SpannedDataGridView casting to datagridview PinmemberSergey Semyonov18-Oct-12 10:46 
NewsHow to make click on any span cell select span correctly & port to VB .NET Pinmemberdrunlar11-Jun-12 8:46 
QuestionLooks like what I want to do PinmemberLee Belcher22-Jan-12 15:17 
AnswerRe: Looks like what I want to do PinmemberSergey Semyonov22-Jan-12 21: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.

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