【问题标题】:How to resize datagrid如何调整数据网格的大小
【发布时间】:2012-09-16 11:42:15
【问题描述】:

我在 winform 中有一个 datagridview,想做两件事。调整数据网格的大小,以便根据数据网格大小显示所有列(无滚动) 调整 winform 的宽度。

  • 尝试了下面的代码,但它不起作用*

        int width = 0; 
        foreach (DataGridViewColumn col in information.Columns)
        {
            width += col.Width;
        }
    
        width += information.RowHeadersWidth;
    
        information.ClientSize = new Size(width + 100,height);
    

【问题讨论】:

  • 我在答案中添加了示例代码,这表明我告诉您的内容在一个简单的示例中有效。如果这对您有帮助,请记得点赞并接受我的回答。

标签: c# winforms datagrid


【解决方案1】:

简单的操作顺序:

  1. 将 DataGridView 的 AutoSizeColumnMode 属性设置为 AllCells。
  2. 添加所有列的 Width 属性以及控件边框的额外宽度等的一些松弛(可能加 2)
  3. 将 DataGridView 的 Width 属性设置为您计算的宽度。
  4. 将表单的宽度设置为 DataGridView 的宽度。

由您自己编写代码。

编辑:我现在在编译器前面,所以我把它放在一起:

进入 Visual Studio。开始一个新项目。不要在设计器的表单上放任何东西。只需在初始化程序中使用此代码即可。

public Form1()
{
    InitializeComponent();

    // Create a DataGridView with 5 Columns
    // Each column is going to sized at 100 pixels wide which is default
    // Once filled, we will resize the form to fit the control
    DataGridView dataGridView1 = new DataGridView();
    for (int i = 0; i < 5; i++)
    {
        DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
        dataGridView1.Columns.Add(col);
    }
    dataGridView1.Location = new Point(0, 0);

    // Add the DataGridView to the form
    this.Controls.Add(dataGridView1);

    // Step 2:
    // Figure out the width of the DataGridView columns
    int width = 0;
    foreach (DataGridViewColumn col in dataGridView1.Columns)
        width += col.Width;
    width += dataGridView1.RowHeadersWidth;

    // Step 3:
    // Change the width of the DataGridView to match the column widths
    // I add 2 pixels to account for the control's borders
    dataGridView1.Width = width + 2;

    // Step 4:
    // Now make the form's width equal to the conbtrol's width
    // I add 16 to account for the form's boarders
    this.Width = dataGridView1.Width + 16;
}

此代码创建一个包含五列的DataGridView,然后完全按照您的要求调整控件和表单的大小。我遵循了上面列出的确切步骤(除了第 1 步,因为我的列中没有任何数据)。

此代码有效。因此,如果您的工作不正常,那么您一定有其他愚蠢的事情发生,我无法帮助您。

【讨论】:

  • 感谢您的快速响应,但我这样做了,但它仍然没有显示所有列 int width = 0; foreach (DataGridViewColumn col in information.Columns) { width += col.Width; } 宽度 += 信息.RowHeadersWidth; information.ClientSize = new Size(width + 100, height +100);
  • 还是不行。列是在运行时创建的,这是一个问题
  • “它不起作用”是什么意思?发生什么了?大小不变吗?你有设置断点吗?在尝试更改控件大小之前,您确定宽度变量是正确的宽度吗?除了“它不起作用”之外,我还需要更多信息。对不起
  • 对不起,我花了大约半小时。无论如何,大小从 756 变为 846,但是仍然没有显示两列。
  • 您是否有为 DataGridView 确定的 MaximumSize?这可能会阻止您根据需要调整它的大小。另一个想法是设置 AutoSizeColumnMode 后,强制 DataGridView 重绘自身。当您获得列的宽度时,可能还没有调整它们的大小。除此之外,我概述的步骤应该足以满足您的需求。我怀疑你还有其他奇怪的事情发生。抱歉,今晚我无法提供更多帮助 - 我正在使用智能手机,并且面前没有编译器。
【解决方案2】:

您好,我发现了如何使用下面的代码来完成这项工作。信息是数据网格,这是表格。

       int width = 0;

        this.information.RowHeadersVisible = false;
        for (int i = 0; i < information.Columns.Count; i++)
            width += information.Columns[i].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);

        int rows = 0;

        this.information.RowHeadersVisible = false;
        for (int i = 0; i < information.Rows.Count; i++)
            rows += information.Rows[i].GetPreferredHeight(i, DataGridViewAutoSizeRowMode.AllCells, true);


        information.Size = new Size(width +20, rows+50);
        this.Width = width + 50;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多