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

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.
column-grouping-and-color.JPG
0nepieceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
lofCommented:
Hi,

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)
as
begin
	select 
		 GroupLabel
		,count(*) as Count
	from (
		select 
			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
			,1
		from Production.Product
	) Tmp 
	where GroupLabel is not null
	group by GroupLabel
end

Open in new window

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
MathiyazhaganCommented:
Hi,
  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
0
 
0nepieceAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.