如果这是DataGridView的容器

发布于 2025-02-04 11:43:25 字数 1993 浏览 3 评论 0原文

我有一个带有tabpage控件的表格。
您可以在附件的图像中看到每个标签都有一个TableLayoutPanel。

tableLayoutPanel具有autoScroll = true(我通过代码禁用水平滚动)。
TableLayoutPanel大小为1209x372像素。

我的tablayoutpanel有1列和6行。
第一行和第三行有一个标签(标题)。
第二行和第四行包含一个datagridview。

带有标签的行的“绝对”大小为21像素。

问题在于托管DataGridView控件的面板的高度。

我尝试使用sizetype设置为自动百分比 ,但是当设置数据源时,面板不会调整到datagridviews的大小。
如果我为每个面板放置一个绝对尺寸,但是我必须尝试使用​​几个像素尺寸。我手动做。

我认为“自动”或“百分比”在这种情况下没有意义的原因是因为数据杂志的高度可能大于TableLayoutPanel控件的高度。
dataGridViews始终具有autoscroll = false

我读到这是优先事项:

当TableLayoutPanel控件安排其行时,它按以下顺序将优先级分配给每个rowstyle:

  1. 行带有Rowstyle设置为Absolute,首先考虑并分配其固定高度。

    >

    >

    >

    >

  2. rowstyle设置以自动化的行具有其内容的大小。

  3. 将剩余的空间分配在行之间,将行列设置为百分比。

然后,我执行此操作(在运行时通过代码):

private void RedimensionaDatagrid(TableLayoutPanel tlp,int indiceRowPanel, DataGridView dtgv)
{
    dtgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    dtgv.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;  
    int alturaTotalDatagrid = 0;
    foreach (DataGridViewRow row in dtgv.Rows)
    {
        alturaTotalDatagrid += row.Height;
    }

    TableLayoutRowStyleCollection styles =
    tlp.RowStyles;
    styles[indiceRowPanel].SizeType = SizeType.Absolute;

    styles[indiceRowPanel].Height = alturaTotalDatagrid;
    dtgv.Dock = DockStyle.Fill;
}

如果该代码段位于form_load事件中,则无法正常工作。

如果我将该代码放入按钮中,则加载了表单并按下按钮,它就可以完美。

我已经在form_show事件中添加了该代码,但是它与form_load相同。

考虑到我使用的:DataGridViewAutoSizerOwsMode.allCells,因此默认高度(22像素)不正确。一旦DataGridView具有数据源,我必须获得每行的结果高度。

我想在不按按钮的情况下,即加载或显示表单时执行此操作(代码段)。

提前致谢。

form with TableLayoutPanel

I have a Form with a TabPage Control.
Each TabPage has a Tablelayoutpanel as you can see in the attached image.

Tablelayoutpanel has AutoScroll = True (I disable the horizontal scroll by code).
Tablelayoutpanel size is 1209x372 pixels.

My Tablelayoutpanel has 1 column and 6 rows.
The first and third row has a label (title) inside.
The second and fourth rows contain a DataGridView.

Rows with a label have a "absolute" size of 21 pixels.

The issue is with the height of the Panels that host the DataGridView Controls.

I have tried with SizeType set to Automatic or Percentage, but the Panels does not resize to the size of the DataGridViews when the DataSource is set.
It works fine if I put an absolute size for each Panel, but then I have to try with several pixel sizes. I do it manually.

I think the reason why "Automatic" or "Percentage" don´t make sense in this case is because the height of the DataGridViews may be greater than the height of the TablelayoutPanel control.
The Datagridviews have AutoScroll = False, always.

I have read that this is the priority:

When the TableLayoutPanel control arranges its rows, it assigns priorities to each RowStyle in the following order:

  1. Rows with RowStyle set to Absolute are considered first and assigned their fixed heights.

  2. Rows with RowStyle set to AutoSize have the size of their content.

  3. The remaining space is divided between rows with the RowStyle set to Percent.

Then I did this (by code in runtime):

private void RedimensionaDatagrid(TableLayoutPanel tlp,int indiceRowPanel, DataGridView dtgv)
{
    dtgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    dtgv.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;  
    int alturaTotalDatagrid = 0;
    foreach (DataGridViewRow row in dtgv.Rows)
    {
        alturaTotalDatagrid += row.Height;
    }

    TableLayoutRowStyleCollection styles =
    tlp.RowStyles;
    styles[indiceRowPanel].SizeType = SizeType.Absolute;

    styles[indiceRowPanel].Height = alturaTotalDatagrid;
    dtgv.Dock = DockStyle.Fill;
}

This does not work fine if that code snippet is located in the form_load event.

If I put that code inside a button, once the form is loaded and I press the button, it works perfect.

I have added that code in the form_Show event but it happens the same that form_load.

Take into account that I use: DataGridViewAutoSizeRowsMode.AllCells, so the default height (22 pixels) is not correct. I must get the result height of each row once the datagridview has DataSource.

I would like to do that action (code snippet) without pressing the button, just when the form is loading or showing.

Thanks in advance.

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

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

发布评论

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

评论(1

醉态萌生 2025-02-11 11:43:25

非常感谢Jimi。
我在运行时(在加载表格和工作过程中)

为DataGridView自动化并为RowPanel自动化。

“ row”面板末端的差距

但是,如果您看到附带的图片,似乎DataGridView在RowPanel的末尾没有结束,存在一点差距。

Thanks a lot Jimi.
I did that in Runtime (during loading form and it works)

AutoSize for DataGridView and AutoSize for RowPanel.

"row" gap in the end of the panel

However, if you see the attached picture, it seems that the DatagridView does not end in the end of the RowPanel, there is a little gap.

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