views:

198

answers:

3

Although the question seems simple, I couldn't find the answer to it.

I have a DBGrid component with lots of columns, and as a result they don't fit into the page and scrollbar appears. I also have column-autofix mechanism, which makes each column have width of the longest element in the table. When I scroll DBGrid to the right-end there is an empty space after the last column. How to get rid of this space?

One solution that I see is to stretch the last column to fit the empty space. But I don't know how to find the length of this empty space! DbGrid.Width and DbGrid.ClientWidth only give length of the component part, but not the real length of the table. Any hints??

+5  A: 

It's not always easy to get DBGrid to behave the way you want it to behave. I decided to use alternative db grids after one project with the standard implementation and I never looked back.

If you can use an alternative grid, you have many options to choose from. There's even a topic here on SO with lots of pointers. Among the free-with-source options, I've always been quite fond of the JVCL project.

Just one last tip: there are grids that offer options and customizing possibilities beyond what you can dream of. Be aware that there's a cost attached to this degree of freedom, e.g. it can make the component slow, hard to integrate in your code, or both.

fvu
+1  A: 

I think I have found a solution (although it seems a little strange). In order to find the difference between column widths and real width of the DBgrid (that means find the width of the empty space left after last column), we need to keep track of which column is shown on the left now (what is current column that is scrolled to). We can do that using OnDrawColumnCell event, since it will draw only columns which are scrolled on now. Then we need to calculate sum of widths of all visible columns, and subtract that from DBGrid's width. P.S. Sorry for bad english

Ex code:

     For i:=0 to Last do
     if Vis[i] then
     Begin
      Sum:=Sum+DBG.Columns[i].Width;
      Inc(Cnt);
     End;

     if dgColLines in DBG.Options then
     Sum := Sum + Cnt;

  //add indicator column width
    if dgIndicator in DBG.Options then
    Sum := Sum + IndicatorWidth;
    Dif:=DBG.ClientWidth - Sum;
Tofig Hasanov
+1  A: 

The below answer is quite good, but not effective at all because it is placed in the OnDrawColumnCell event, so if there are a large number of rows and/or collumns, the performance might fall.

Instead of that, place the same code not in a painting event, but int the AfterOpen (and AfterRefresh) event of the source dataset in order to execute it only once for each resultset.

roboflekto