.net 如何更改 DataGridView 列的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20152844/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:33:01  来源:igfitidea点击:

How to change width of DataGridView Columns

.netvb.netdatagridviewwidth

提问by stackexchange12

I have a vb.net windows forms application with a datagridview. I'm hoping to find a way to change the width for multiple columns in a datagridview. This is what I have so far, but it only changes the width for the first column not the rest. I appreciate any help you may give.

我有一个带有 datagridview 的 vb.net windows 窗体应用程序。我希望找到一种方法来更改 datagridview 中多列的宽度。到目前为止,这是我所拥有的,但它只更改了第一列的宽度,而不是其余列的宽度。我感谢您提供的任何帮助。

Dim columnwidth1 As DataGridViewColumn = DataGridView1.Columns(0)
        columnwidth1.Width = 100

Dim columnwidth2 As DataGridViewColumn = DataGridView1.Columns(1)
        columnwidth2.Width = 100

Dim columnwidth3 As DataGridViewColumn = DataGridView1.Columns(2)
        columnwidth3.Width = 100

Dim columnwidth4 As DataGridViewColumn = DataGridView1.Columns(3)
        columnwidth4.Width = 100

回答by ??ssa P?ngj?rdenlarp

Check that you do not have code to change it back (perhaps in the ColumnWidthChangedevent); and check if AutoSizeColumnsModeis on. Also, you can iterate columns rather than do it individually:

检查您是否没有将其改回的代码(可能在ColumnWidthChanged事件中);并检查是否AutoSizeColumnsMode打开。此外,您可以迭代列而不是单独执行:

For Each c As DataGridViewColumn In DataGridView1.Columns
    c.Width = 50
Next