C# 如何更改 DataGridView 中的列宽?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11926534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change column width in DataGridView?
提问by tylerbhughes
I have created a database and table using Visual Studio's SQL Server Compact 3.5 with a dataset as my datasource. On my WinForm I have a DataGridView with 3 columns. However, I have been unable to figure out how to get the columns to take up the full width of the DataGridView which is shown in the image below.
我使用 Visual Studio 的 SQL Server Compact 3.5 创建了一个数据库和表,并将数据集作为我的数据源。在我的 WinForm 上,我有一个包含 3 列的 DataGridView。但是,我一直无法弄清楚如何让列占据 DataGridView 的整个宽度,如下图所示。


I want to make the abbreviates column wider and then have the description column extend all the way over to the edge of the form. Any suggestions?
我想让缩写列更宽,然后将描述列一直延伸到表单的边缘。有什么建议?
Update:
更新:
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;
}
}
}
采纳答案by hmqcnoesy
You could set the width of the abbrev column to a fixed pixel width, then set the width of the description column to the width of the DataGridView, minus the sum of the widths of the other columns and some extra margin (if you want to prevent a horizontal scrollbar from appearing on the DataGridView):
您可以将 abbrev 列的宽度设置为固定像素宽度,然后将描述列的宽度设置为 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
If you wanted the description column to always take up the "remainder" of the width of the DataGridView, you could put something like the above code in a Resizeevent handler of the DataGridView.
如果您希望描述列始终占据 DataGridView 宽度的“剩余部分”,您可以将类似上述代码的内容放在ResizeDataGridView的事件处理程序中。
回答by MShahid777
Set the "AutoSizeColumnsMode" property to "Fill".. By default it is set to 'NONE'. Now columns will be filled across the DatagridView. Then you can set the width of other columns accordingly.
将“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
回答by Mogeeb Aljamali
In my Visual Studio 2019 it worked only after I set the AutoSizeColumnsModeproperty to None.
在我的 Visual Studio 2019 中,它仅在我将AutoSizeColumnsMode属性设置为None.

