Click here to Skip to main content
12,694,121 members (32,620 online)
Click here to Skip to main content
Add your own
alternative version


94 bookmarked

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

You may also be interested in...


Comments and Discussions

GeneralRe: double buffer for the datagridview Pin
Member 1052976523-Apr-14 0:57
professionalMember 1052976523-Apr-14 0:57 
QuestionVBNet 2010 Pin
beto.silva.araujo@gmail.com30-Oct-12 14:08
memberbeto.silva.araujo@gmail.com30-Oct-12 14:08 
AnswerRe: VBNet 2010 Pin
Sergey Semyonov31-Oct-12 3:42
memberSergey Semyonov31-Oct-12 3:42 
GeneralRe: VBNet 2010 Pin
beto.silva.araujo@gmail.com31-Oct-12 8:20
memberbeto.silva.araujo@gmail.com31-Oct-12 8:20 
AnswerRe: VBNet 2010 Pin
H Lugo1-Jan-14 9:26
memberH Lugo1-Jan-14 9:26 
QuestionDataGridViewButtonCellEx ? Pin
Stefan Gies18-Oct-12 9:25
memberStefan Gies18-Oct-12 9:25 
AnswerRe: DataGridViewButtonCellEx ? Pin
Sergey Semyonov18-Oct-12 10:33
memberSergey Semyonov18-Oct-12 10:33 
QuestionBinding Pin
Krysiu_2926-Sep-12 20:08
memberKrysiu_2926-Sep-12 20:08 

A i've got problem with span behavior after binding to datgridview. I definied 2 columns of type DataGridVewTextBoxCell in datgidview and populate data by this source code (I have express edition 2010):

private void wypelnijGrid4()
            Konfiguracja konf = new Konfiguracja();
            string connStr = konf.GetConnectionString();
            string sql = "select id,vakt from VERS";
            OracleConnection conn = new OracleConnection();
            conn.ConnectionString = connStr;
            sCommand = new OracleCommand(sql, conn);
            sAdapter = new OracleDataAdapter(sCommand);
            sBuilder = new OracleCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "vers");
            sTable = sDs.Tables["vers"]; 
            dataGridView4.AutoGenerateColumns = false;
            dataGridView4.DataSource = sDs.Tables["vers"]; 

            var cell = (DataGridViewTextBoxCellEx)dataGridView4[0, 3];
            cell.ColumnSpan = 2;
            cell.RowSpan = 2; 

Binding works and I see data in columns but span behaviuor doesn't work. Without databinding when I add rows manually (using datgridview.rows.add()) then code
var cell = (DataGridViewTextBoxCellEx)dataGridView4[0, 3];
            cell.ColumnSpan = 2;
            cell.RowSpan = 2;

works corectlly - any idea how can I solve the problem ?
AnswerRe: Binding Pin
Sergey Semyonov18-Oct-12 10:42
memberSergey Semyonov18-Oct-12 10:42 
Questionso good Pin
Member 437754611-Sep-12 22:54
memberMember 437754611-Sep-12 22:54 
QuestionSpannedDataGridView casting to datagridview Pin
ajayarav5-Jun-12 21:58
memberajayarav5-Jun-12 21:58 
AnswerRe: SpannedDataGridView casting to datagridview Pin
Krysiu_2926-Sep-12 19:24
memberKrysiu_2926-Sep-12 19:24 
AnswerRe: SpannedDataGridView casting to datagridview Pin
Sergey Semyonov18-Oct-12 10:46
memberSergey Semyonov18-Oct-12 10:46 
NewsHow to make click on any span cell select span correctly & port to VB .NET Pin
drunlar11-Jun-12 8:46
memberdrunlar11-Jun-12 8:46 
QuestionLooks like what I want to do Pin
Lee Belcher22-Jan-12 15:17
memberLee Belcher22-Jan-12 15:17 
AnswerRe: Looks like what I want to do Pin
Sergey Semyonov22-Jan-12 21:10
memberSergey Semyonov22-Jan-12 21:10 
GeneralRe: Looks like what I want to do Pin
nimolZero24-May-12 0:27
membernimolZero24-May-12 0:27 
Questiondoublebuffering Pin
xboxproject19-Apr-11 2:16
memberxboxproject19-Apr-11 2:16 
AnswerRe: doublebuffering Pin
Sergey Semyonov25-Apr-11 1:32
memberSergey Semyonov25-Apr-11 1:32 
GeneralRe: doublebuffering Pin
xboxproject26-Apr-11 2:08
memberxboxproject26-Apr-11 2:08 
AnswerRe: doublebuffering Pin
Sergey Semyonov27-Apr-11 5:30
memberSergey Semyonov27-Apr-11 5:30 
GeneralRe: doublebuffering Pin
tipsybroom5-Jun-13 5:21
membertipsybroom5-Jun-13 5:21 
Generallast question for this fabulous control :-) Pin
gertdebusser16-Dec-10 0:53
membergertdebusser16-Dec-10 0:53 
GeneralRe: last question for this fabulous control :-) Pin
Sergey Semyonov16-Dec-10 4:18
memberSergey Semyonov16-Dec-10 4:18 
GeneralRe: last question for this fabulous control :-) Pin
Sergey Semyonov21-Dec-10 3:08
memberSergey Semyonov21-Dec-10 3:08 

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.

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