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.

A little knowledge and creativity

There are a lot of grid components out there, all with a long list of advanced features for you to play with. But there are some issues with these grids making them unwanted for private use. First of all, lots of them are data aware. Personally I like to hold my data in objects and streams where I can easily remain control. Secondly, some of them represent a huge overkill of what I usually am in need of. And third, the price... $350 is quite much to pay for a grid when all you do is playing around at the boy's room making shareware for friends.

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-makeup

Have 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.