.net DataGridView - 如何设置列宽?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2154154/
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
DataGridView - how to set column width?
提问by Captain Comic
I have a WinForms application with DataGridViewcontrol. My control has five
columns (say "Name", "Address", "Phone" etc)
我有一个带DataGridView控件的 WinForms 应用程序。我的控件有五列(比如“姓名”、“地址”、“电话”等)
I am not happy with default column width. I want to have more control over column appearance. What I want is to be able to do one of the following:
我对默认列宽不满意。我想对列的外观有更多的控制。我想要的是能够执行以下操作之一:
- Set width of each column in percent
- Set width of each column in pixels
- Use some other best-practive method (make width to fit text etc)
- 以百分比设置每列的宽度
- 以像素为单位设置每列的宽度
- 使用其他一些最佳实践方法(使宽度适合文本等)
Please suggest - which property to use and how.
请建议 - 使用哪个属性以及如何使用。
回答by Bhaskar
You can use the DataGridViewColumn.Width property to do it:
您可以使用 DataGridViewColumn.Width 属性来做到这一点:
DataGridViewColumn column = dataGridView.Columns[0];
column.Width = 60;
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width.aspx
回答by Mark
The following also can be tried:
也可以尝试以下方法:
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
or use the other setting options in the DataGridViewAutoSizeColumnsModeEnum
或使用DataGridViewAutoSizeColumnsMode枚举中的其他设置选项
回答by Mark Lakata
Most of the above solutions assume that the parent DateGridViewhas .AutoSizeModenot equal to Fill. If you set the .AutoSizeModefor the grid to be Fill, you need to set the AutoSizeMode for each columnto be Noneif you want to fix a particular column width (and let the other columns Fill). I found a weird MS exception regarding a null object if you change a Column Width and the .AutoSizeModeis not Nonefirst.
上述大部分解决方案的假设父DateGridView已经.AutoSizeMode不等于Fill。如果.AutoSizeMode将网格设置为Fill,则需要将每列的 AutoSizeMode 设置为,None如果要固定特定的列宽(并让其他列填充)。如果您更改列宽并且.AutoSizeMode不是None第一个,我发现了一个关于空对象的奇怪 MS 异常。
This works
这有效
chart.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
... add some columns here
chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
chart.Column[i].Width = 60;
This throws a null exception regarding some internal object regarding setting border thickness.
这将引发有关设置边框厚度的某些内部对象的空异常。
chart.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
... add some columns here
// chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
chart.Column[i].Width = 60;
回答by Jeremy L
I know this is an old question but no one ever answered the first part, to set width in percent.That can easily be done with FillWeight(MSDN). In case anyone else searching comes across this answer.
我知道这是一个老问题,但没有人回答过第一部分,以百分比设置宽度。这可以通过FillWeight(MSDN)轻松完成。万一其他搜索者遇到这个答案。
You can set DataGridAutoSizeColumnMode to Fill in the designer. By default that gives each column FillWeight of 100. Then in code behind, on FormLoad event or after binding data to grid, you can simply:
您可以在设计器中将 DataGridAutoSizeColumnMode 设置为 Fill。默认情况下,每列的 FillWeight 为 100。然后在后面的代码中,在 FormLoad 事件上或在将数据绑定到网格之后,您可以简单地:
gridName.Columns[0].FillWeight = 200;
gridName.Columns[1].FillWeight = 50;
And so on, for whatever proportional weight you want. If you want to do every single column with numbers that add up to 100, for a literal percent width, you can do that too.
依此类推,对于您想要的任何比例权重。如果你想用加起来为 100 的数字来处理每一列,对于文字百分比宽度,你也可以这样做。
It gives a nice full DataGrid where the headers use the whole space, even if the user resizes the window. Looks good on widescreen, 4:3, whatever.
它提供了一个很好的完整 DataGrid,其中标题使用整个空间,即使用户调整窗口大小也是如此。在宽屏上看起来不错,4:3,无论如何。
回答by hawbsl
Regarding your final bullet
关于你最后的子弹
make width fit the text
使宽度适合文本
You can experiment with the .AutoSizeMode of your DataGridViewColumn, setting it to one of these values:
您可以尝试使用 DataGridViewColumn 的 .AutoSizeMode,将其设置为以下值之一:
None
AllCells
AllCellsExceptHeader
DisplayedCells
DisplayedCellsExceptHeader
ColumnHeader
Fill
More info on the MSDNpage
MSDN页面上的更多信息
回答by kindaska
i suggest to give a width to all the grid's columns like this :
我建议为所有网格的列提供一个宽度,如下所示:
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "phone"
col.Width = 120;
col.DataPropertyName = (if you use a datasource)
thegrid.Columns.Add(col);
and for the main(or the longest) column(let's say address) do this :
对于主要(或最长)列(假设地址),请执行以下操作:
col = new DataGridViewTextBoxColumn();
col.HeaderText = "address";
col.Width = 120;
tricky part
棘手的部分
col.MinimumWidth = 120;
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
tricky part
棘手的部分
col.DataPropertyName = (same as above)
thegrid.Columns.Add(col);
In this way, if you stretch the form (and the grid is "dock filled" in his container) the main column, in this case the address column, takes all the space available, but it never goes less than col.MinimumWidth, so it's the only one that is resized.
这样,如果您拉伸表单(并且网格在其容器中“停靠”),则主列(在本例中为地址列)将占用所有可用空间,但它永远不会小于 col.MinimumWidth,因此它是唯一一个调整大小的。
I use it, when i have a grid and its last column is used for display an image (like icon detail or icon delete..) and it doesn't have the header and it has to be always the smallest one.
我使用它,当我有一个网格并且它的最后一列用于显示图像(如图标详细信息或图标删除..)并且它没有标题时,它必须始终是最小的。
回答by santosh
public static void ArrangeGrid(DataGridView Grid)
{
int twidth=0;
if (Grid.Rows.Count > 0)
{
twidth = (Grid.Width * Grid.Columns.Count) / 100;
for (int i = 0; i < Grid.Columns.Count; i++)
{
Grid.Columns[i].Width = twidth;
}
}
}
回答by Mahesh
Use:
用:
yourdataView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
回答by Nate S.
I you dont want to do it programmatically, you can manipulate to the Column width property, which is located inside the Columns property.Once you open the column edit property you can choose which column you want to edit, scroll down to layout section of the bound column properties and change the width.
我不想以编程方式执行此操作,您可以操作位于 Columns 属性内的 Column width 属性。打开列编辑属性后,您可以选择要编辑的列,向下滚动到布局部分绑定列属性并更改宽度。
回答by Schpenn
DataGridView1.Columns.Item("Adress").Width = 60
DataGridView1.Columns.Item("Phone").Width = 30
DataGridView1.Columns.Item("Name").Width = 40
DataGridView1.Columns.Item("Etc.").Width = 30

