| Professional Looking Grids with Custom Drawing | ||||||||||||||||||||||||
| Page 1: How to fix-up the column headers. | ||||||||||||||||||||||||
Article submitted by Vegar Vikan for the Delphi Programming Quickies Contest - Round #2 Winner!
Using custom drawing to make your grids look as nice as the expensive third party suites. Three main topics are presented: how to fix-up the column headers, how to add clickable buttons and checkboxes inside cells and how to simulate merged cells.
What I will try to show you here, is that with a little knowledge and creativity, you can turn Delphi's own grids into nice, professional looking grids. There are three main topics that I will show you. First of all we should take a look on how to fix-up the column header. Then I will show how to add clickable buttons and checkboxes inside cells. And last I will show a way to simulate merged cells. Below I have included a screenshot from the sample application that comes with the article, showing some ideas of what you can do with a standard TStringGrid.
![]() Doing some header-makeupHave you ever noticed how the FixedRows-property is set to zero when the number of rows goes below two? That means that you loose the 3D-look, and you want get it back if you add some rows. You manually have to set the FixedRows property back after adding more rows. We will make a two-step solution for this. First we have to make sure the FixedRows property is updated when changing RowCount. Second we have to make sure the header keeps a consistent 3D-look.I usually have a 'UpdateGrid()'-method that I call after adding or deleting data records. First of all this method updates the RowCount property. Then it checks the FixedRows property, and finally invalidates the grid. As you can see from the code listing, the first step is rather trivial. In a real-world situation I would have read the row count from the data holder, but here I have passed it as a parameter since we don't have any data.
The next step is the drawing and involves some more work. First of all we calculate the position for the text. Then we set all the properties of the canvas object and clear the cell. Next thing we use the DrawText method provided with the Windows API. This method has some very usable flags that we can provide. Take a look at the documentation to get a complete description. Here I have used four flags that give us a single line of text, centered both vertical and horizontal and with an ellipse ('...') at the end if the cell is too small to fit all of the text. At the end of the method, we draw the lines around the cell to simulate a 3D-look.
It is possible to include components for editing inside cells, but if a checkbox or a simple button is all you need you can easily do it with some custom drawing... Next page > How to add clickable buttons and checkboxes inside cells > Page 1, 2, 3 |
||||||||||||||||||||||||

