How to group results by column and how to change color of the first column

Posted on 2009-12-28
Medium Priority
Last Modified: 2013-12-17
Hi, please refer to the image. I want to include that header section to group results according column. How do I do that? Also, this datagridview is blank and later on filled, how do I set the first column to have a particular background color like in the example? I'm using Windows Forms in C# .NET. Thanks in advance.
Question by:0nepiece
LVL 10

Accepted Solution

lof earned 500 total points
ID: 26135981

DataGridView does not support grouping so I suggest you do it on the database level with dynamic grouping. Attached is a sample code based on AdventureWorks database that does dynamic grouping without dynamic SQL - part of an article I'm preparing to publish soon.

To change a colour of a column use this snippet

dataGridView1.Columns["columnname"].DefaultCellStyle.BackColor = Color.Red

and if you want to change colour for certain cells only you will have to handle CellPainting event and add code like that:

if (someCondition) e.CellStyle.BackColor = Color.Green;

create procedure Production.ProductSummary @groupBy varchar(32)
		,count(*) as Count
	from (
			case upper(@groupBy)
				when 'MAKEFLAG' then convert(varchar(1),MakeFlag)
				when 'COLOR' then Color
				when 'PRODUCTLINE' then ProductLine
				when 'TOTAL' then 'Total'
				else null
			end as GroupLabel
		from Production.Product
	) Tmp 
	where GroupLabel is not null
	group by GroupLabel

Open in new window

LVL 10

Assisted Solution

Mathiyazhagan earned 500 total points
ID: 26136006
  as Lof said ,datagridview does not support column groupping.but, you can do this by developing user control.Have a look on this article :http://www.codeproject.com/KB/grid/OutlookGrid.aspx

Author Closing Comment

ID: 31670653

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question