如何使 TDBGrid 的列适合网格的宽度?

发布于 2024-08-16 16:02:49 字数 152 浏览 19 评论 0原文

我有一个带有预定义列的 TDBGrid,但我无法获得正确的宽度。我可以在表单设计器中弄乱列的宽度属性,并使宽度在设计时看起来恰到好处,但在运行时,无论出于何种原因,列往往会明显更宽,最终会出现滚动条在网格的底部。有没有什么方法可以使列的大小正确,而无需反复试验,特别是如果网格只有一两个列?

I've got a TDBGrid with predefined columns, and I just can't get the width right. I can mess with the Width property of the columns in the Form Designer and get the width to look just right at design time, but at runtime, for whatever reason, the columns tend to be significantly wider, and I end up with a scroll bar at the bottom of the grid. Is there any way to get make the columns size themselves correctly without all the trial-and-error, especially if the grid only has one or two of them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只等公子 2024-08-23 16:02:49

我在表单中使用这个程序 OnResize 事件

procedure AutoStretchDBGridColumns(Grid: TDBGrid; Columns, MinWidths: Array of integer);
var
  x, i, ww: integer;
begin
  // Stretches TDBGrid columns
  // Columns contains columns to stretch
  // MinWidths contains columns minimum widhts
  // To stretch grids columns 1,2 and 5 automatically and set minimum widths to 80, 150 and 150 call
  // AutoStretchDBGridColumns(DBGrid1, [1,2,5], [80, 150, 150]);
  Assert(Length(Columns) = Length(MinWidths), 'Length(Columns) <> Length(MinWidths)');
  ww := 0;
  for i := 0 to Grid.Columns.Count - 1 do
  begin
    if Grid.Columns[i].Visible then
      ww := ww + Grid.Columns[i].Width + 1; //** +1 for grid line width
  end;
  if dgIndicator in Grid.Options then
    ww := ww + IndicatorWidth;
  x := (Grid.ClientWidth - ww) div Length(Columns);
  for i := 0 to  High(Columns) do
    Grid.Columns[Columns[i]].Width := Max(Grid.Columns[Columns[i]].Width + x, MinWidths[i]);
end;

它有一些缺陷,但效果很好。

I use this procedure inside forms OnResize event

procedure AutoStretchDBGridColumns(Grid: TDBGrid; Columns, MinWidths: Array of integer);
var
  x, i, ww: integer;
begin
  // Stretches TDBGrid columns
  // Columns contains columns to stretch
  // MinWidths contains columns minimum widhts
  // To stretch grids columns 1,2 and 5 automatically and set minimum widths to 80, 150 and 150 call
  // AutoStretchDBGridColumns(DBGrid1, [1,2,5], [80, 150, 150]);
  Assert(Length(Columns) = Length(MinWidths), 'Length(Columns) <> Length(MinWidths)');
  ww := 0;
  for i := 0 to Grid.Columns.Count - 1 do
  begin
    if Grid.Columns[i].Visible then
      ww := ww + Grid.Columns[i].Width + 1; //** +1 for grid line width
  end;
  if dgIndicator in Grid.Options then
    ww := ww + IndicatorWidth;
  x := (Grid.ClientWidth - ww) div Length(Columns);
  for i := 0 to  High(Columns) do
    Grid.Columns[Columns[i]].Width := Max(Grid.Columns[Columns[i]].Width + x, MinWidths[i]);
end;

It has some flaws, but works quite well.

岁月染过的梦 2024-08-23 16:02:49

我想我知道问题的答案。几年来我也遇到了同样的问题。
当我放入演示数据时,我调整了列宽度(设计时),但我怀疑,作为 Delphi 中的一个错误,如果您输入的宽度与 Delphi 分配给列的默认宽度相同,则宽度不会保存在dfm 文件 - 如果您以文本形式查看 dfm,则可以看到这一点。

我的解决方案是将其设置为宽或窄一像素,以便 Delphi 将被迫存储宽度(或直接编辑 dfm),因此它将在运行时设置。如果您不设置它,Delphi 将在运行时分配一些计算的宽度 - 这不是您想要的,因为它可能会根据数据而改变。简单的解决方案,但我花了很长时间才弄清楚。

I think I know the answer to the problem. I had the same problem for a few years.
When I put demo data in I resize the columns width (design time) but I suspect, as a bug in Delphi, if the width you enter is the same as the default width Delphi assigns to the column, the width is not saved in the dfm file - as can be seen if you view the dfm as text.

My solution is to set it to one pixel wider or narrower so that Delphi will be forced to store the width (or edit the dfm directly) and so it will be set at run time. If you don't set it, Delphi will assign some calculated width at run time - which is not what you want as it could change depending on the data. Simple solution but it took me a long time to figure out.

半透明的墙 2024-08-23 16:02:49

您可以这样做,但必须检查可能可以显示的所有记录所有字段的宽度。

鉴于仍然有大量应用程序很难过滤其记录集,这可能意味着您需要检查无数记录,只是为了摆脱滚动条。

您是否曾经考虑过您可能只想一次显示太多信息?

可能的替代方案是为不适合其列的字段显示提示文本?

我已经使用了这样的替代方案,但需要检查来源;明天我可以给你一些示例代码。

——杰罗恩

You can do that, but you have to check the width of all fields for all records that you potentially can display.

Given the fact that there are still plenty of applications that hardly filter their record set, this can mean that you need to check a zillion records, just to get rid of the scrollbar.

Have you ever considered that you might just want to display too much information at once?

A possible alternative is to show a hint text for fields that do not fit in their column?

I have used such an alternative, but need to check the sources; I can get you some sample code tomorrow.

--jeroen

ぃ双果 2024-08-23 16:02:49

JP的回答是一个相当不错的解决方案。您需要根据需要手动拉伸或收缩列以使其适合窗口。有一个 Windows API 调用来获取垂直滚动条宽度。然后您可以查看可见行以查看是否显示垂直滚动条。

另一种技术是计算出每列相对于彼此的宽度。然后,当您调整大小时,它们保持相同的比例。

JP's answer is a pretty good solution. You need to manually stretch or shrink the columns as necessary to make it fit in the window. There is a windows API call to get the vertical scroll bar width. Then you can look at the visible rows to see if the vertical scroll bar is being shown.

Another technique is to figure out the width of each column relative to each other. Then when you resize them maintain the same ratio.

两个我 2024-08-23 16:02:49

您可以查看问题的答案。

也许受保护的 CalcDrawInfo 方法可能会有所帮助。

You may have a look at the answers to this question.

Perhaps the protected CalcDrawInfo method may help here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文