WinForms Grid – How to Custom Draw Card Captions
In this video, you will learn how to implement code to apply custom painting to Card captions in a grid control.
- Starting from a new WinForms application, I drop a grid control onto the form and dock it to its parent.
- I bind the grid to a data source by invoking its smart tag and clicking the “Add Project Data Source” link.
- I’ll use an existing Connection String for the Northwind sample database.
- I click “Next” and select the “Orders” Table that will supply data to the grid.
- Finally, I click finish to complete the wizard.
- For this example, I need to change the current view to a Card View.
- I click on the “Click here to change view” link and select “Card View”.
- Now my data is represented as cards.
- To apply custom painting to the Card Captions, I’ll need to handle the CustomDrawCardCaption event.
- This event is raised each time a card caption needs to be repainted.
- I select the card view and create the event handler for it.
- First, I’m going to add references to the following namespaces.
- Back in the event handler, I’ll add the following code to custom paint the card captions.
- Notice that I’m using different brushes for focused and non-focused cards.
- I run the application to see the results.
- You can see that the first card automatically has focus, therefore it’s painted with the blue gradient.
- When I click on a different card, the focus is changed and the CustomDrawCardCaption event is raised to repaint the card captions.
The CustomDrawCardCaptions event can also be used to change the formatting of the text as well as add icons or images to the card captions.
Thanks for watching and thank you for choosing DevExpress!