如果这是DataGridView的容器
我有一个带有tabpage控件的表格。
您可以在附件的图像中看到每个标签都有一个TableLayoutPanel。
tableLayoutPanel具有autoScroll = true
(我通过代码禁用水平滚动)。
TableLayoutPanel大小为1209x372
像素。
我的tablayoutpanel有1列和6行。
第一行和第三行有一个标签(标题)。
第二行和第四行包含一个datagridview。
带有标签的行的“绝对”大小为21
像素。
问题在于托管DataGridView控件的面板的高度。
我尝试使用sizetype
设置为自动
或百分比
,但是当设置数据源时,面板不会调整到datagridviews的大小。
如果我为每个面板放置一个绝对尺寸,但是我必须尝试使用几个像素尺寸。我手动做。
我认为“自动”或“百分比”在这种情况下没有意义的原因是因为数据杂志的高度可能大于TableLayoutPanel控件的高度。
dataGridViews始终具有autoscroll = false
。
我读到这是优先事项:
当TableLayoutPanel控件安排其行时,它按以下顺序将优先级分配给每个rowstyle:
行带有Rowstyle设置为Absolute,首先考虑并分配其固定高度。
>>>>rowstyle设置以自动化的行具有其内容的大小。
将剩余的空间分配在行之间,将行列设置为百分比。
然后,我执行此操作(在运行时通过代码):
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具有数据源,我必须获得每行的结果高度。
我想在不按按钮的情况下,即加载或显示表单时执行此操作(代码段)。
提前致谢。
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:
Rows with RowStyle set to Absolute are considered first and assigned their fixed heights.
Rows with RowStyle set to AutoSize have the size of their content.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常感谢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.