You are on page 1of 12

12,186,039 members (62,195 online)

Sign in

Search for articles, questions, tips

home

articles

quick answers

discussions

Ask a Question

features

community

All Questions All Unanswered

help

FAQ

How can i merge DataGridView


Rows (Cells with Equal Values)
See more:

C#

DataGridView

Rate this:

Is embodiment available like that picture?


<img src="http://marss.co.ua/temp/img/merge-rows.gif"></img>

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Top Experts
Posted 30-Jan-11 23:52pm Edited 31-Jan-11 1:14am v2
gksdy2000 306

Last 24hrs

This month

OriginalGriff

254 OriginalGriff

1,028

KARTHIK Bang 150 KARTHIK Bang


Add a Solution

Dave Kreskowiak

jitendra2009 2 9-May-12 8:02am

80 Sascha Lefvre

355
315

Also Read

no

Apache Spark: Creating


Value, Action, and Innovation

killer_ism 6-Mar-13 1:00am


Sorry for my English... How fix a bug, w hen I use horizontal scrolling?

Solution 1

421

Afzaal Ahmad Z 75 F-ES Sitecore

Comments

5 solutions

110 phil.o

Jochen Arndt

594

Top Rated

Most Recent

Using Apache Hadoop with


IBM BigInsights to Deliver
Value Quickly

Rate this:

I think, it will be better if you merge tables first and then bind Gridview to
that table.

Posted 31-Jan-11 0:05am


open in browser PRO version Are you a developer? Try out the HTML to PDF API

Related Questions
Merging Excel file Rows on Equal value through
C#
How to merge rows with equal values in a
pdfcrowd.com

Posted 31-Jan-11 0:05am


Pooja90 404

gridview for window application


merge rows by columns in datagridview

Comments

datagridview cell merge


some thing wrong when I merge cells by row

gksdy2000 31-Jan-11 6:13am

C# Datagridview row cells value windows


application

Sorry. this is DataGridView in w informs.

Merge cells

Solution 8

Regarding Merging Cells in Datagridview Cell


paint Event
Rate this:

DataGridView row disable depending on 1 cell


value

I modified and it works


( set DataGridViewCellStyle { WrapMode=True })

How to merge cells with equal values in a


gridview for window application
Hide Expand

Copy Code

void Compare()
{
DataTable dt = GetDataTable();
DataView dv = new DataView(dt);
String strAtual = String.Empty;
foreach (DataRowView row in dv)
{
if
(strAtual.Equals(row.Row["OrderId"].ToString()))
{
foreach (DataColumn dataColumn in
dv.Table.Columns)
{
String columnName =
(dataColumn.ColumnName);
if (columnName != "OrderId")
{

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

string strTagNumb =
row[columnName].ToString();
CompareDelete(strTagNumb, strAtual,
dv, columnName);
}
}
row.Delete();
continue;
}
if
(!string.IsNullOrEmpty(row.Row["OrderId"].ToString()))
strAtual = row.Row["OrderId"].ToString();
}
dataGridView2.DataSource = dv;
}
Posted 12-Feb-15 20:25pm
private void
Member 10554801
457 CompareDelete(string strTagNumb, string
strAtual, DataView dt, String columnName)
{
foreach (DataRowView row in dt)
{
if
(row.Row["OrderId"].ToString().Equals(strAtual))
{
string nl = Environment.NewLine;
Rate this:
row.Row[columnName] += string.Concat(nl,
strTagNumb);
return; I provided you with links in my answer
Thanks for the good question.
}
above. But I've also tried to solve this problem by myself.
}
The following
code demonstrates how you can merge cells and keep it
}

Solution 5

megred even if user selects merged cell.


Merging mode will be turned on after button "Merge" is clicked.
This is just the EXAMPLE(wrote by 10-15 minutes
) of possible
solution - improve it as you wish.
Hide Expand

Copy Code

using System;

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

using
using
using
using
using
using

System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Text;
System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
DataGridView dataGrid;
DataSet dataSet;
Button button;
private List<string> MergedRowsInFirstColumn = new
List<string>();
private bool readyToMerge = false;
public Form1()
{
InitializeComponent();
CreateGridAndButton();
}
private void CreateGridAndButton()
{
dataGrid = new DataGridView();
dataSet = new DataSet();
dataGrid.Height = this.Height - 100;
dataGrid.Dock
= DockStyle.Top;
Posted 1-Feb-11
10:31am
dataGrid.ReadOnly
= true;
Tim__James 1.6K
dataGrid.AllowUserToAddRows = false;
dataGrid.AllowUserToResizeRows = false;
dataGrid.RowHeadersVisible = false;
Comments
this.dataGrid.Paint += new
PaintEventHandler(dataGrid_Paint);
gksdy2000 1-Feb-11 18:47pm

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

gksdy2000 1-Feb-11 18:47pm


Oh~ This is code that I find.
It became big help.
Thank.

Tim ur Akhm edov 2-Feb-11 1:36am


You are w elcome :)

Rashrak 27-May-14 3:45am


Hi Timur Akhmedov , Thank you soo much , Your code helped me a lot. But I'm
facing a problem.My datagridview have a column w ith checkbox , w hen I merge
the cells of that column it show s only value like 'True'/'False'. My requirement is to
show a single checkbox column for the entire row that I merged. Hope you got
w hat I asked.

IndrajitDasgupat 13-Aug-12 1:28am


If Data Grid View generating in Run Time Auto generated it is show ing error in your
code in the below line
else
{
MergeCells(Row sToMerge[0], Row sToMerge[1], dataGrid.Columns["Rack"].Index,
isSelectedCell(Row sToMerge, dataGrid.Columns["Rack"].Index) ? true : false);
CollectMergedRow sInFirstColumn(Row sToMerge[0], Row sToMerge[1]);
Row sToMerge[0] = -1;
}

Solution 4

Rate this:

Hi,
I did it a few weeks ago, follow the cobe below:
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Hide Expand

Copy Code

using (DataTable dt = new DataTable())


{
adapter.Fill(dt);
DataView dv = new DataView(dt);
String strAtual = String.Empty;
//Percorrer o DataTable
foreach (DataRowView row in dv)
{
if
(strAtual.Equals(row.Row["OrderId"].ToString()))
{
string strTagNumb =
row["TagList"].ToString();
CompareDelete(strTagNumb, strAtual,
dv);
row.Delete();
continue;
}
if
(!string.IsNullOrEmpty(row.Row["OrderId"].ToString()))
strAtual =
row.Row["OrderId"].ToString();
}
intTotal = dv.Count;
gvSchedules.DataSource = dv;
gvSchedules.DataBind();
}
private void CompareDelete(string strTagNumb, string
strAtual, DataView dt)
{
Hope that foreach
helps. (DataRowView row in dt)
{
felipekm.com if (row.Row["OrderId"].ToString().Equals(strAtual))
{
row.Row["TagList"] += string.Concat("</br>",

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Posted 31-Jan-11 3:15am


felipekm 886

Comments
Timur Akhmedov 31-Jan-11 9:22am
Could you elaborate how does it w ork? I don't see cells merging here...
-Timur

Felipe Kautzm ann de Mello 31-Jan-11 11:00am


Sure,
Actually in this case I did merged Row s.
You have to iterate into the cell, saving the first one a some variable, after that you have
to match this variable (previous data) w ith the actual iterate data, if this is true, you have
to concatenate and delete the row , implement the same logic w ith these cells like I did.
If you need a w orkaround please post your code.
Cheers,

gksdy2000 31-Jan-11 16:57pm


Maybe, is this grid in Web?
Grid of the question is in Winforms.

Felipe Kautzm ann de Mello 31-Jan-11 20:45pm


Yes, it is.
But the logic still the same. If you post your code w ill be better.

IndrajitDasgupat 13-Aug-12 1:42am


gvSchedules w hat is this in your code please define

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

middha.ram 28-Jun-13 5:01am


w hen i use scroll bar for datagridview then its not w orking

Solution 3

Rate this:

Merging cells in DataGridView is difficult thing because there are no


standard ways to do this:
http://social.msdn.microsoft.com/forums/enUS/winformsdatacontrols/thread/fea3dcff-5447-450a-8f7cbb1c4f63371d/[^]
Here is solution I found:
http://www.codeguru.com/forum/showthread.php?t=415930[^]
But if you still want to do this - algorithm should be easy:
1. Merge cells in First column
2. Merge cells in rest of columns which have the same cell in first column
Hope this will help
-Timur

Posted 31-Jan-11 2:16am


Tim__James 1.6K

open in browser PRO version

Edited 31-Jan-11 2:19am v2

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Add your solution here


B

S small BIG code var <

>

& link [^] encode untab

case indent outdent

Preview

Existing Members
open in browser PRO version

...or Join us
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Existing Members

...or Join us

Sign in to your account

Download, Vote, Comment, Publish.

Your Email

Your Email

Password

Optional Password
Forgot your password?
I have read and agree to the
and Privacy Policy
Please subscribe me to the CodeProject
newsletters

Submit your solution!


When answering a question please:
1. Read the question carefully.
2. Understand that English isn't everyone's first language so be
lenient of bad spelling and grammar.
3. If a question is poorly phrased then either ask for clarification,
ignore it, or edit the question and fix the problem. Insults
are not welcome.
Let's work to help developers, not make them feel stupid.

This content, along with any associated source code and files, is licensed under The
Code Project Open License (CPOL)

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Advertise | Privacy | Mobile


Web02 | 2.8.160402.1 | Last Updated 13 Feb 2015

Layout: fixed | fluid

Copyright CodeProject, 1999-2016


All Rights Reserved. Terms of Service

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

You might also like