DataGridView 的行为就好像它在 TableLayoutPanel 中具有最小尺寸一样
DataGridView acts as though it has a minimum size inside a TableLayoutPanel
当我在表单上放置数据网格视图时,使用 Dock = DockStyle.Fill
,我可以在数据网格视图滚动条出现任何问题之前将表单缩小到只剩下不到一行。但是,如果我将它放在一个 table 布局面板中,该面板依次停靠到表单,数据网格视图开始表现得好像它具有最小高度,切断底部行和滚动条底部。
public Form1()
{
InitializeComponent();
BindingList<Widget> list = new BindingList<Widget>();
dataGridView1.DataSource = list;
for (int i = 0; i < 100; i++)
{
list.Add(new Widget { MyProperty = i });
}
}
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.Controls.Add(this.dataGridView1, 0, 1);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
//
// dataGridView1
//
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(976, 518);
this.Controls.Add(this.tableLayoutPanel1);
this.tableLayoutPanel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
我尝试添加 this.tableLayoutPanel1.AutoScroll = true;
结果显示数据网格没有调整大小,面板滚动条正确放置并且没有被截断。
没有对我有用的答案的类似问题:
- flowlayout panel not shrinking when parent form size is changed
- Winforms DataGridView resizing issue
在 InitializeComponent
中添加这一行,它应该可以让网格缩小到零,让滚动条按预期显示。
this.dataGridView1.Size = new System.Drawing.Size(0, 0);
我通过在将 datagridview 放入 tablelayoutpanel 之前将其包装在面板中来解决这个问题。
奇怪的是,这并没有发生在我放入 tablelayoutpanels 的所有数据网格视图中。
当我在表单上放置数据网格视图时,使用 Dock = DockStyle.Fill
,我可以在数据网格视图滚动条出现任何问题之前将表单缩小到只剩下不到一行。但是,如果我将它放在一个 table 布局面板中,该面板依次停靠到表单,数据网格视图开始表现得好像它具有最小高度,切断底部行和滚动条底部。
public Form1()
{
InitializeComponent();
BindingList<Widget> list = new BindingList<Widget>();
dataGridView1.DataSource = list;
for (int i = 0; i < 100; i++)
{
list.Add(new Widget { MyProperty = i });
}
}
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.Controls.Add(this.dataGridView1, 0, 1);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
//
// dataGridView1
//
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(976, 518);
this.Controls.Add(this.tableLayoutPanel1);
this.tableLayoutPanel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
我尝试添加 this.tableLayoutPanel1.AutoScroll = true;
结果显示数据网格没有调整大小,面板滚动条正确放置并且没有被截断。
没有对我有用的答案的类似问题:
- flowlayout panel not shrinking when parent form size is changed
- Winforms DataGridView resizing issue
在 InitializeComponent
中添加这一行,它应该可以让网格缩小到零,让滚动条按预期显示。
this.dataGridView1.Size = new System.Drawing.Size(0, 0);
我通过在将 datagridview 放入 tablelayoutpanel 之前将其包装在面板中来解决这个问题。
奇怪的是,这并没有发生在我放入 tablelayoutpanels 的所有数据网格视图中。