-1

I have a grid that can be resized. And i'm now stuggeling with filling the blank space around columns in the grid. I'm trying to achieve this on FormResize.

First i calculate what is the total of columns width and then i'm comparing it to the string grid width. if the stringgrid widths is bigger then i add to each columns width equal portions of the blank space left. This is how it looks in formResize Procedure:

procedure TBDDTool.FormResize(Sender: TObject);
var
  totColWidth,i : integer;
begin
  totColWidth := 0;
  for i := 0 to sgFilePreview.ColCount - 1 do
    totColWidth := totColWidth + sgFilePreview.ColWidths[i];
  if sgFilePreview.Width > TotColWidth then
    begin
      for i := 0 to sgFilePreview.ColCount - 1 do
        begin
          sgFilePreview.ColWidths[i] := round(sgFilePreview.ColWidths[i] +
                ((sgFilePreview.Width - totColWidth)/(sgFilePreview.colCount)));
        end;
    end;

end;

This actualy doesn't work cause sgFilePReview.Width is the width of my grid. And i don't know how to get the width of the whole space inside the grid, like every columns + blank space left. How can i get the real width of the grid? Cause sgFilePreview.Width return the width of the grid but as seen from outside the grid.

Thank you!

EDIT

Space

Addine new columns

          for val in sLineSplitted do
        begin
          if Pos('#',val) <> 0 then propVal := copy(val,0,pos('#',val)-1)
          else propVal := val;
          col := col +1;
          if (row = 1) then
            begin
              if (col >1) then
      //Add column
              sgFilePreview.ColCount := col;
              sgFilePreview.Cols[col-1].Text := propVal;
              SetLength(aSourceData[row-1],col);
              aSourceData[row-1,col-1] := val;
            end
          else
            begin
            sgFilePreview.RowCount := row;
              SetLength(aSourceData[row-1],col);
              aSourceData[row-1, col-1] := val;
              sgFilePreview.Cells[col-1, row-1] := propVal;
              pnlFileManager.Visible := true;
            end;


        end;

Auto size columns to fit word if the world is bigger than the cell's width

    procedure TBDDTool.AutoSizeGrid(Grid: TStringGrid);
const
  ColWidthMin = 10;
var
  C,R,W, ColWidthMax: integer;
begin

  for c := 0 to Grid.ColCount - 1 do
    begin
      ColWidthMax := ColWidthMin;
      for R := 0 to Grid.RowCount - 1 do
        begin
          W := Grid.Canvas.TextWidth(Grid.Cells[C,R]);
          if W > ColWidthMax then
            ColWidthMax :=W;
        end;
        Grid.ColWidths[C] := ColWidthMax +5;
    end;


end;    
Community
  • 1
  • 1
user28470
  • 419
  • 5
  • 17
  • Don't you use `ClientWidth`? – David Heffernan Sep 30 '14 at 09:49
  • @DavidHeffernan I used clientWidth but it returns the same value as gird.Width would return – user28470 Sep 30 '14 at 09:50
  • In that case I don't understand your question – David Heffernan Sep 30 '14 at 09:58
  • @DavidHeffernan I have a blank space at the end of my grid. After all the columns. And in order to get rid of that space i wanted to add this space equally to all the columns so there no more space left. I don't want to see that space that's why i'm trying to do this – user28470 Sep 30 '14 at 10:00
  • I guess you need to account for the border lines and the between column lines too. I'd start with the total width. Then remove all border/divider lines from that total, and that leaves the width available. Then go over each column setting the width to WidthAvailable div ColCountRemaining. As you do each column, decrement WidthAvailable by the width that you just used, and decrement ColCountRemaining by 1. – David Heffernan Sep 30 '14 at 10:03
  • But how do i know if there's space available? I can now what's the total width of my columns. But if i don't know how to get the actual inner size of my grid i can't know how much space left :( – user28470 Sep 30 '14 at 10:05
  • It is the width of the control minus the width of the borders and lines. Assuming that `ColWidths[]` specifies the pixel width of the section between the vertical lines. – David Heffernan Sep 30 '14 at 10:06
  • I don't get it. Why calculating the vertical lines width? I don't see how it gives me info about the space left – user28470 Sep 30 '14 at 10:09
  • @DavidHeffernan oh, the width of the control is 490 pixels, and all my columns are more than 4000 pixels. I'm searching for a way to get the real space inside the control like 4000 pixels + pixels of the blank space. And the width of the control only gives me the real wids as seen from outside, only 490 pixels – user28470 Sep 30 '14 at 10:18
  • So i guess this is impossible x) – user28470 Sep 30 '14 at 10:24
  • I'm sure it's not impossible. But I still don't understand what you are trying to do. Clearly what I've described above is well wide of what you are trying to do. I cannot understand you. So I cannot help you. Maybe somebody else can understand you. Or maybe you need to explain yourself better. – David Heffernan Sep 30 '14 at 10:49
  • @DavidHeffernan If yo uscroll horizontally on a grid that has enough columns allowing you to scroll. You will find out that you can scroll further than the last column. After the last column you have a big space of blank. I want to remove this space. Idk how i can explain better than this. – user28470 Sep 30 '14 at 10:55
  • @DavidHeffernan http://i.stack.imgur.com/LQrYm.png – user28470 Sep 30 '14 at 10:56
  • @DavidHeffernan in this image his grid is small enough so you don't scroll on this grid. But in mine grid you can scroll and at the end you have this blank space. – user28470 Sep 30 '14 at 10:57
  • @DavidHeffernan Width only gives the visible Space that a grid take. In my case 490 pixel. I can scroll inside the grid for more than 4000 pixels. After the 4000pixels there's like 500pixels of blank – user28470 Sep 30 '14 at 10:58
  • I don't understand you at all. And you seem to be putting all the details in various comments. It's no fun trying to piece all this together from comments. Please edit the question. As it stands I cannot help, and attempting to do so is just wasting my time. – David Heffernan Sep 30 '14 at 10:58
  • I think you don't even try to understand, thank you for your comments. – user28470 Sep 30 '14 at 10:59
  • I think that @user28470 is trying to find out the size of the entire scrolable area of its controll, so he can adjust the ColWidth so that there is no empty region ot the right of the grid. – SilverWarior Sep 30 '14 at 11:18
  • Yeah, it isn't hard to understand. Idk why he acts like it's super complicated to understand :/ – user28470 Sep 30 '14 at 11:19
  • @user28470 That is probably becouse only recently you mentioned that your columns doesn't even fit into your control visible area at once so you have to scroll left and rgiht. Now if your columns would fit into visible area you could use GridWidth to get the curent width of all the columns combined. http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.Grids.TCustomDrawGrid.GridWidth But unfortunately this doesent work if your grid exceeds the visible area of your controll. Infact in such case there should be no empty space at the end, since the scrollable area auto adjusts to the grid size. – SilverWarior Sep 30 '14 at 11:25
  • Are you maybe overriding default croling. If so than this might be the prbolem as it could scroll to much toward the right. – SilverWarior Sep 30 '14 at 11:26
  • @SilverWarior I'm adding my columns via code and i don't override the scrollbar. Could it be because my delphi is 2006? – user28470 Sep 30 '14 at 11:27
  • @user28470 In that case it would be good if you could show us your code for adding columns as this would hopefully alow us to recreate the problem you are expiriencing. So I recomend you edit your question by adding this code to id. Also don't forget to add the information that the number and size of the columns is to big for all to be shown at the same time. – SilverWarior Sep 30 '14 at 11:32
  • @SilverWarior there i edit it showing an image of what it looks like, how i'm adding columns and a procedure i'm using when a word is bigger than it cells to resize the column – user28470 Sep 30 '14 at 11:43
  • @SilverWarior So is it possible? If you check here: http://www.asiplease.net/computing/delphi/string_grid_component.htm Everyone have blank space in their string grid – user28470 Sep 30 '14 at 12:02
  • The reason why there is empty space on every image shown in the lnk you supplied is that in one of those pcitures the combined width of all columns isn't greater than the width of client area of the StringGrid controll like it is in your case. – SilverWarior Sep 30 '14 at 12:22
  • @SilverWarior ok, it's not my case cause my columns are bigger. I can't find any information on the internet on how to get it done :/ – user28470 Sep 30 '14 at 12:25
  • I also don't know the answer from my head, but if I'm not mistaken in one of my projects I do add columns programatically and I don't have empty space on the right side. So I'll take a look at it when I get home and let you know what I found out. – SilverWarior Sep 30 '14 at 12:27
  • @SilverWarior ok tank you :) – user28470 Sep 30 '14 at 12:30
  • Well I just checked the program I was talking about and I use different component there. I use TListView with vsReport style to achieve similar look. Anywhay I have done some testings and I can athleast explain why this problem is occuring to you and a posible solution althou it small chance that it will sutie you as it requires all cols to have same width to work. So check my answer below. – SilverWarior Sep 30 '14 at 18:18

1 Answers1

2

The main problem why these empty spaces are occurring to you even when you have too many columns so that all of them can be seen at the same time is the fact that in StringGrid scrolling works a bit different than you are used to in other controls.

When you scroll around in StringGrid the scrolling position is always aligned to the position of TopLeft visible cell. So if the combined width of visible cols isn't the same as ClientWidth this means that you will either have partially visible col at the right side or and empty space when you have scrolled all the way to the right.

Now one possible way to avoid this is to resize the columns so that they always fit into the client width (no partially visible columns). But the problem is that this becomes practically impossible if you have different widths for each column.

In case if you can live with the fact that all columns will have same width you can use the code below which works in most cases. It isn't perfect because you can only set column width to integer values where sometimes you would need larger precision.

procedure TForm1.FormResize(Sender: TObject);
var cwDefaultWidth: Integer;
    VisibleCols: Integer;
    ColWidth: Integer;
begin
  cwDefaultWidth := 64;
  VisibleCols := StringGrid1.ClientWidth div cwDefaultWidth;
  if VisibleCols >= StringGrid1.ColCount then
  begin
    ColWidth := Round(StringGrid1.ClientWidth / StringGrid1.ColCount-1);
  end
  else
  begin
    ColWidth := Round(StringGrid1.ClientWidth / VisibleCols-1);    
  end;
  StringGrid1.DefaultColWidth := ColWidth;
end;

But if you are using variable column widths then the only thing that you could do is adjust the size of the last column so that it's width fills the empty space that would otherwise appear.

In order to do that you would first have to check to see if you are scrolled fully to the right. Then you would have to sum up the width of currently seen columns. You could do this by using:

for I := StringGrid1.LeftCol to StringGrid1.RowCount-1 do
begin
  VisibleColsWidth := VisibleColsWidth + StringGrid1.ColWidths[I];
end;

Then you subtract this width from StringGrid1.ClientWidth and you get the width of empty space. So finally you increase the size of last column for the empty space width.

I really hope that even if my answer doesn't provide you with an actual solution it would at least guide you towards finding the right solution.

w5m
  • 2,286
  • 3
  • 34
  • 46
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • I'll try your proposition tomorrow i don't have acces to the code at home. I'll use the first solution so my columns will look formatted and actually it would look prettier if they all have the same width i suppose. Thank you for the time take to answer me, i'll try your solution tomorrow as ssoon as i can. – user28470 Sep 30 '14 at 21:18
  • There's always blank space. I've tried both methods. I think it can't be fixed. In the second one the size of the last column take the blank space but after this another blank space is added so I start to think Delphi is a troll haha. Anyway i'll let it with blank space haha. Thank for your concerning :) – user28470 Oct 01 '14 at 05:42
  • It is posible that the math doesen't work corectly and thus makes cols width a bit to big. In case of second approach you could try manually reducing the new size of last column by a few pixels and see the result. – SilverWarior Oct 01 '14 at 06:33
  • Yeah i've tried putting my size manually but it always adds an blank space at the end. Anyway i'll just leave it like that. It's not that important. – user28470 Oct 01 '14 at 06:38