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

Posted on 2009-12-28
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


    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

      as Lof said ,datagridview does not support column groupping.but, you can do this by developing user control.Have a look on this article :

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    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.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now