如何在DataGridView中更改列宽?

22

我使用Visual Studio的SQL Server Compact 3.5和数据集作为数据源创建了一个数据库和表格。在我的WinForm中,我有一个带有3列的DataGridView。然而,我一直无法弄清楚如何使列占据整个DataGridView的宽度,如下图所示:

我想使缩写列更宽,并且让描述列延伸到表单的边缘。有什么建议吗?

更新:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuickNote
{
public partial class hyperTextForm : Form
{
    private static hyperTextForm instance;

    public hyperTextForm()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Normal;
        this.MdiParent = Application.OpenForms.OfType<Form1>().First();            
    }

    public static hyperTextForm GetInstance()
    {
        if (instance == null || instance.IsDisposed)
        {
            instance = new hyperTextForm();
        }

        return instance;
    }

    private void abbreviationsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.abbreviationsBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.keywordDBDataSet);
    }

    private void hyperTextForm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'keywordDBDataSet.abbreviations' table. You can move, or remove it, as needed.
        this.abbreviationsTableAdapter.Fill(this.keywordDBDataSet.abbreviations);
        abbreviationsDataGridView.Columns[1].Width = 60;
        abbreviationsDataGridView.Columns[2].Width = abbreviationsDataGridView.Width - abbreviationsDataGridView.Columns[0].Width - abbreviationsDataGridView.Columns[1].Width - 72;
    }
}
}
3个回答

33

您可以将缩写列的宽度设置为固定像素宽度,然后将描述列的宽度设置为DataGridView的宽度减去其他列的宽度之和以及一些额外的间距(如果您想要防止DataGridView出现水平滚动条):

dataGridView1.Columns[1].Width = 108;  // or whatever width works well for abbrev
dataGridView1.Columns[2].Width = 
    dataGridView1.Width 
    - dataGridView1.Columns[0].Width 
    - dataGridView1.Columns[1].Width 
    - 72;  // this is an extra "margin" number of pixels

如果您想让描述列始终占据DataGridView宽度的“剩余”部分,可以在DataGridView的Resize事件处理程序中放置类似上面代码的内容。


我将它添加到我的表单加载函数中,以便在表单加载时自动调整大小,但出于某种原因它根本不会调整大小。我甚至尝试将其放在表单的构造函数中,但没有成功。 - pallasmedia
嗯...你有任何隐藏的列吗?如果你愿意发布你的代码,我可以仔细看一下。 - hmqcnoesy
1
不,那些是我唯一拥有的列。我不知道我的代码在哪里对你有用,因为我是通过设计器创建了所有内容的。我在这个表单中实际上只有你给我的那段代码。我已经删除了我尝试过的所有其他代码。 - pallasmedia
3
你的DataGridView是否将AutoSizeColumnsMode属性设置为非“None”值?这会阻止你的代码设置列宽。 - hmqcnoesy
1
是的,谢谢。我完全忘记了我之前做过那个。我还有一个问题。我当前在我的数据库中有9个项目,当我在IDE内外运行程序并向数据库添加一个新项目时,id从来没有像应该的那样从10开始。相反,它从-1开始。你有什么想法为什么会这样? - pallasmedia
你的数据库表的 id 列是自增类型吗?这样可以让数据库处理自增,而不是依赖应用程序分配值。你可能需要开一个新问题。 - hmqcnoesy

6
将"AutoSizeColumnsMode"属性设置为"Fill"。默认情况下,该属性设置为“NONE”。现在,列将填充整个DataGridView。然后,您可以相应地设置其他列的宽度。
DataGridView1.Columns[0].Width=100;// The id column 
DataGridView1.Columns[1].Width=200;// The abbrevation columln
//Third Colulmns 'description' will automatically be resized to fill the remaining 
//space

3

在我的 Visual Studio 2019 中,只有在我将 AutoSizeColumnsMode 属性设置为 None 后才能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接