问题描述
我正在尝试根据 DataGridView 包含的行数设置它的高度.目前,我能够通过以下行完成此操作:
I am trying to make the height of my DataGridView AutoSize based on the amount of rows it contains. Currently, I was able to accomplish this with the following line:
dataGridView_SearchResults.AutoSize = true;
然而这会使水平滚动条消失,DataGridView 被切断.
However this makes the Horizontal scroll bar disappear, the the DataGridView gets cut off.
如何在不丢失水平滚动条的情况下自动调整高度?
How can I autosize the height without losing the horizontal scroll bar?
推荐答案
选项 1 - 覆盖 GetPreferredSize
您可以覆盖 GetPreferredSize
方法 DataGridView
并使用新的建议大小调用基础方法 new Size(this.Width, proposalSize.Height)
.这样,控件的当前宽度将保持不变,而自动调整大小规则将应用于其高度:
You can override GetPreferredSize
method of DataGridView
and call the base method using new proposed size new Size(this.Width, proposedSize.Height)
. This way, the current width of control will remain untouched while the auto-size rules will apply on its height:
using System.Drawing;
using System.Windows.Forms;
public class MyDataGridView : DataGridView
{
public override Size GetPreferredSize(Size proposedSize)
{
return base.GetPreferredSize(new Size(this.Width, proposedSize.Height));
}
}
选项 2 - 根据计算的自动大小的高度设置高度
如果你不想从 DataGridView
派生,你可以通过调用它的 GetPreferredSize
传递 new Size(0, 0) 来计算自动大小
然后将DataGridView
的高度设置为result的高度,这样你只需要改变DataGridView
的高度.您应该在 RowsAdded
、RowsRemoved
、其他一些事件(如果需要)中设置自动高度:
If you don't want to derive from DataGridView
, you can calculate the auto-size by calling its GetPreferredSize
passing new Size(0, 0)
then set the height of DataGridView
to the height of result, this way you only change the height of DataGridView
. You should set the auto-height in RowsAdded
, RowsRemoved
, some other events if you need:
void AutoHeightGrid(DataGridView grid)
{
var proposedSize = grid.GetPreferredSize(new Size(0, 0));
grid.Height = proposedSize.Height;
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.RowsAdded += (obj, arg) => AutoHeightGrid(dataGridView1);
dataGridView1.RowsRemoved += (obj, arg) => AutoHeightGrid(dataGridView1);
//Set data source
//dataGridView1.DataSource = something;
}
如果您想确保网格中的所有更改包括更改字体
、行高都会导致调整网格大小,您可以在Paint
事件中调用该方法.
If you want to make sure that all changes in grid including changing Font
, height of rows will cause resizing grid, you can call the method in Paint
event.
选项 3 - 设置 MaximumSize
同样如 Hans 所提到的,如果您不想从 DataGridView
派生,您可以使用网格的 MaximumSize
属性.您可以将其设置为 new Size(this.dataGridView1.Width, 0)
:
Also as mentioned by Hans, if you don't want to derive from DataGridView
, you can use MaximumSize
property of the grid. You can set it to new Size(this.dataGridView1.Width, 0)
:
dataGridView1.MaximumSize = new Size(this.dataGridView1.Width, 0);
dataGridView1.AutoSize = true;
注意
由于使用MaximumSize
不是那么友好,当用户希望通过左右锚点让网格宽度改变时,我更喜欢使用Option 1或选项 2.
Since using MaximumSize
is not so friendly when the user wants to let the grid width change by left and right anchors, I prefer to use Option 1 or Option 2.
这篇关于DataGridView 自动高度 - 如何自动调整 DataGridView 高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!