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.

  1. Starting from a new WinForms application, I drop a grid control onto the form and dock it to its parent.
  2. I bind the grid to a data source by invoking its smart tag and clicking the “Add Project Data Source” link.
  3. I’ll use an existing Connection String for the Northwind sample database.
  4. I click “Next” and select the “Orders” Table that will supply data to the grid.
  5. Finally, I click finish to complete the wizard.
  6. For this example, I need to change the current view to a Card View.
  7. I click on the “Click here to change view” link and select “Card View”.
  8. Now my data is represented as cards.
  9. To apply custom painting to the Card Captions, I’ll need to handle the CustomDrawCardCaption event.
  10. This event is raised each time a card caption needs to be repainted.
  11. I select the card view and create the event handler for it.
  12. First, I’m going to add references to the following namespaces.
  13. Back in the event handler, I’ll add the following code to custom paint the card captions.
  14. Notice that I’m using different brushes for focused and non-focused cards.
  15. I run the application to see the results.
  16. You can see that the first card automatically has focus, therefore it’s painted with the blue gradient.
  17. 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!