How to merge cells in DataGridView?

UncleRonin

Well-known member
Joined
Feb 28, 2006
Messages
230
Location
South Africa
Programming Experience
5-10
I want to merge cells but I have no idea how... the best solution I've heard of is somehow changeing the Paint method of the first cell and painting it across the grid... how do you do this? is there any other way to merge cells?
 
There is no support for merging cells in the DataGridView or DataGrid controls, and this may sound rude, but I'd say there is no need for this functionality - ever. That is, unless you want to build an Excel application replica, in which case you could easily build your own "datagrid" control too.
 
Merge columns in DataGridView

That's certainly one of the more short-sighted and arrogant responses I've heard from a moderator. Enough to inspire me register just so I could reply! There is a legitimate need for this functionality. It makes it very convenient to navigate and manage large amounts of data structured in a fixed-depth tiered hierarchy. I need the same thing for one of my projects. Worked great with FlexGrid in VB6.
 
Well, I still don't see the need for a single input control to function as a paper report printout. By all means, I favour hierarchic and common-world layed out input schemas on forms, but not in a table format such as the grids - that does only make it helter-skelter.

As I said and stand by, there is no need for this functionality except if you are creating a Office Excel application where user is to design their own input and presentation surface through this single "control".

Welcome to the forum, there is something good about everything.. ;)
 
Merge columns

I have a need - it may be a niche need. I have a 3-tier hierarchy. It represents a common advertiser structure: parent-division-brand. This equates to PepsiCo:Frito Lay:Fritos. When organized in this fashion, my database has about 50k records. To visually navigate these records in a merged view (like excel pivot-tables) is much easier on the eyes than an unmerged view, where the only break-indicator is the text itself. Make any sense? I know you may say use the TreeView, but in a huge fixed-depth hierarchy, that thing is just as awkward. Not trying to be competitive, just want to find a solution for this project.
 
How is merging cells in a DGV not useful? Having cells which can span(merge) multiple rows or columns helps make grid/table layouts far more attractive and user-friendly. Maybe you've just never had the need for this functionality?

I'm busy revamping the project I originally needed this functionality for and to use a DGV with spanning would be fantastic. I've been forced to use Owc11.AxSpreadsheet control and it is not a pleasure to use! You need the user to have Office installed and managing/manipulating the 'control' is a bastard to learn, especially since most of us don't know what all the HRESULT errors actually mean (and searching for them on the internet turns up only a few useless hits). A spanning DGV would provide better memory management, better future support (since activex controls are not recommended - hence .NET), better programmability for new users, etc. It would be better alround.

A home-made datagrid can also be done but not all of us have the expertiese nor the time to develop one.

If MS developed a DGV which allowed us to merge cells I could bet my life's income that it WOULD be used ALOT. Its Web application would be enormous and would help us steer clear of Win forms with these ugly archaic style grids. I don't remember anyone ever making a rule saying that a grid HAD to be completely uniform.

>_< My thoughts on the matter. I mean, seriously, just think of the applications for this!

*wanders off dreaming of not having to use Ax wrappers ever again*
 
Suppose one want to merge Cell 2 and 3 in Row 0 of a Datagrid(dg_ESC) then type following code in Cell Painting event of the Datagrid


Private Sub dg_ESC_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dg_ESC.CellPainting

If e.RowIndex = 0 AndAlso (e.ColumnIndex = 2 Or e.ColumnIndex = 3) Then
Using gridBrush As Brush = New SolidBrush(Me.dg_ESC.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As Pen = New Pen(gridBrush)
' Clear cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
'Bottom line drawing
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
'top line drawing
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Top)
'Drawing Right line
If e.ColumnIndex = 3 Then
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
End If
'Inserting text
If e.ColumnIndex = 3 Then
e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X - (e.CellBounds.X / 4), e.CellBounds.Y + 5)
End If
e.Handled = True
End Using
End Using
End If

This solve's the problem...

Thanking

Rakesh Sharma,

Jodhpur INDIA
 
MergeRow was VERY useful

We use a flexgrid in our VB6 app to show the user (loan servicer) a customer's loan payment history. Most rows would contain date due, date paid, interest paid, principal paid, principal balance, etc.

Some rows would contain loan servicer notes: "Mr Jones says August payment will be late due to medical emergency. Karen says do not assess late fee.", etc.

Rows containing notes would need to be merged in order for the complete note to appear on the row(s).

Not having this ability in the Datagridview control is a real issue for us.
 
Last edited:
Rows containing notes would need to be merged in order for the complete note to appear on the row(s).
I totally do not agree with you on that. Merging columns is not the solution to make data in a single column more visible, that would break the purpose of the tabular grid control. If the column is not wide enough you have to make it wider. Tooltips may also help if you all too much data in a column for it to be displayed within the available bounds, this is something that is already built into the DataGridView control and enabled by default (ShowCellToolTips property).
One alternative may also be that you display such extended information in a labels/textboxes bound to same bindingsource, to be shown when a specific row is selected.
 
I've gotta say that after all the time that's passed since my original post the merging functionality I needed all those years ago would still have made a fantastic difference and even though I'm now at a level (finally! :3) where I'm able to inherit from the DGV and implement my own control myself it should really be included as part of default behaviour ;)

It's not written in the rules that a datagrid cannot have spanning/merged cells and it's really incredibly useful. Obviously it's best use is for reporting since the grouping and layout mechanism is great but even for grid based input and editing solutions it's really nifty. There are of course ways to get around spanning/merging cells but those are normally uglier and normally require additional user interaction such as hovering, selecting, etc. whereas with merged cells all the necessary information is displayed all at once with more flexibility. And if you want tooltips or further info to be displayed as well, without the spanning/merging things start getting even uglier and you'll need space consuming labels/popups/controls all over the place.

The goal is not to have an Excel replica or replacement but just to have the additional functionality to enhance and improve usability and display. The DGV is by far one of my most favourite controls (ignoring the performance hits and workarounds for large data sets) and I use it in almost every application I develop. Merging is not a necessity but it's awesome and a big reason 3rd party grids sell in the first place.

I don't expect to see it any time soon but one can dream... ;)
 
Back
Top