Friday 18 July 2008

Quick tip 2: Enable TDBGrid to show all columns without scrolling

One of my friends called me and asked me today how to enable TDBGrid in Borland C++ Builder to resize it’s columns on showing data in it.
He had a problem with TDBGrids that need to show some tables or query results with large number of columns.
He needed all of them to be visible without horizontal scrolling.
As I am aware, this function does not exist in TDBGrid, and it has to be made.
If it does exist I’m really not aware of it, and please tell me about it.
So, I wrote him code that I often used and what worked well for me :

I made a function called AdjustGrid() which will iterate through all columns and set their width to some equal value. This way, we can always see all the columns in some DBGrid , almost regardless to it’s size.
Let’s look at the example I made. Button1 just shows the data in the DBGrid, without adjusting any size ( default ).


and when we click on Button2, we get:


…And – it helped him… ;-) ! He even told me he will buy me a beer! ( psss: he never does! ;-) )
…and why am I writing about this trivial thing?
Because I remembered than that many people asked me the same thing before. I hope this will helpful to some of you how are looking for something like this…

No comments: