Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Tip/Trick

Color Block Overlay for DataGridView

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
27 May 2014CPOL2 min read 18.3K   1K   10   6
This component allows to overlay grid with colored blocks spanning across multiple columns.

control screenshot

Introduction

This component allows to overlay grid with colored blocks spanning across multiple columns. Color block is bound to a particular row through unique id and it is possible to resize/move these blocks. Hope this helps someone having similar requirements.

Implemented features are listed below:

  • select
  • resize: ability to validate resize operation + ColorBlockResized event with cancel support
  • drag & drop: Per-cell granularity to allow/deny drop + ColorBlockMoved event with cancel support
  • color blocks can overlap: Color block with biggest starting column will be drawn on top
  • color blocks with colspan bigger than available area on grid
  • text in color block
  • scrolling with frozen columns
  • sort/group operations of the underlying grid
  • resizing columns
  • image at the beginning of the block

Using the Code

As I prefer pictures more than text or code, let me begin with a diagram showing the component organization:

Image 2

Overlay component is abstracted from the grid therefore, providing you can implement IOverlaidGrid interface which is the contact point with underlying grid, it is possible to integrate it with any grid. Originally, it was developed to use over different grid and only for the purpose of this tip, it was integrated with the standard .NET DataGridView. Integration of component can be done through inheritance or composition, in demo inheritance is used - DataGridViewEx. Main class handling color block rendering is GridOverlayRenderer. This class needs a reference to object implementing IOverlaidGrid and to control over which drawing is performed. It provides Render method which renders configured color blocks. In the example, this method is called in Paint event of DataGridViewEx. Color blocks can be configured via InitializeOverlay method of GridOverlayRenderer - see OnLoad in Form1. For further details, please refer to the attached source code.

References

In the tip, I used the work of others. Here are references to the original articles:

License

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


Written By
Software Developer
Slovakia Slovakia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhelp to understand the code Pin
Member 1466157229-Nov-19 18:29
Member 1466157229-Nov-19 18:29 
QuestionThanks and question Pin
Member 1200812825-Apr-17 22:46
Member 1200812825-Apr-17 22:46 
QuestionThank You Pin
Member 1131388215-Jun-15 23:21
Member 1131388215-Jun-15 23:21 
QuestionNice! Pin
Volynsky Alex28-May-14 0:23
professionalVolynsky Alex28-May-14 0:23 
AnswerRe: Nice! Pin
Matus Kovacik28-May-14 7:50
Matus Kovacik28-May-14 7:50 
Thank you Smile | :)
QuestionRe: Nice! Pin
Volynsky Alex28-May-14 8:20
professionalVolynsky Alex28-May-14 8:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.