DataGridView自动高度 - 如何AutoSize DataGridView高度? [英] DataGridView Auto Height - How to AutoSize DataGridView Height?

查看:303
本文介绍了DataGridView自动高度 - 如何AutoSize DataGridView高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据它包含的行数量,使DataGridView AutoSize的高度。目前,我可以通过以下一行来实现:

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,proposedSize.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 到结果的高度,这样你只能更改 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;
}

如果要确保网格中的所有更改,包括更改 Font ,行的高度将导致调整网格大小,您可以调用 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 在用户希望通过左右锚点更改网格宽度时不太友善,所以我更喜欢使用选项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自动高度 - 如何AutoSize DataGridView高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆