DataGridView自动填充和填充 [英] DataGridView AutoFit and Fill

查看:423
本文介绍了DataGridView自动填充和填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 DataGridView 中有3列。我想要做的是将前2列自动适应内容的宽度,并将第3列填充剩余空间。



是否可以在WinForms中做?如果有任何用途,我正在从EF DataContext加载我的数据。我已经包含了当前的外观形象。



解决方案

您需要使用 DataGridViewColumn.AutoSizeMode 属性。



您可以对列0和1使用以下值之一:


AllCells 列宽调整为适合
列中所有单元格的内容,包括标题单元格。


AllCellsExceptHeader: 列宽调整为适合列中所有单元格的内容,不包括标题单元格。

DisplayedCells: 列宽度调整为
,以适合列中显示在屏幕上的行当前
的列中的所有单元格的内容,包括标题单元格。


DisplayedCellsExceptHeader: 列宽调整为适合显示在屏幕上的行当前
的列中所有单元格的
内容,不包括标题单元格。 em>


然后使用第2列的填充


列宽调整,使所有列的宽度完全填充控件的显示区域...




  this.DataGridView1.Columns [0] .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; 
this.DataGridView1.Columns [1] .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
this.DataGridView1.Columns [2] .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;






如其他用户所指出的,默认值可以使用 datagridview 级别设置%28v = vs.110%29.aspx> DataGridView.AutoSizeColumnsMode 属性。

  this.DataGridView1.Columns [0] .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; 
this.DataGridView1.Columns [1] .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

可以是:

  this.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; 






重要提示:



如果您的网格绑定到数据源,并且列是自动生成的( AutoGenerateColumns 属性设置为True)您需要使用 DataBindingComplete 事件应用样式 AFTER 列已创建。






在某些情况下(例如通过代码更改单元格值),我不得不调用 DataGridView1 .AutoResizeColumns(); 刷新网格。


I have 3 columns in my DataGridView. What I am trying to do is have the first 2 columns auto fit to the width of the content, and have the 3rd column fill the remaining space.

Is it possible to do in WinForms? I am loading my data from an EF DataContext if that's any use. I have included an image of how it currently looks.

解决方案

You need to use the DataGridViewColumn.AutoSizeMode property.

You can use one of these values for column 0 and 1:

AllCells: The column width adjusts to fit the contents of all cells in the column, including the header cell.
AllCellsExceptHeader: The column width adjusts to fit the contents of all cells in the column, excluding the header cell.
DisplayedCells: The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, including the header cell.
DisplayedCellsExceptHeader: The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, excluding the header cell.

Then you use the Fill value for column 2

The column width adjusts so that the widths of all columns exactly fills the display area of the control...

this.DataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
this.DataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
this.DataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


As pointed out by other users, the default value can be set at datagridview level with DataGridView.AutoSizeColumnsMode property.

this.DataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
this.DataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

could be:

this.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;


Important note:

If your grid is bound to a datasource and columns are auto-generated (AutoGenerateColumns property set to True), you need to use the DataBindingComplete event to apply style AFTER columns have been created.


In some scenarios (change cells value by code for example), I had to call DataGridView1.AutoResizeColumns(); to refresh the grid.

这篇关于DataGridView自动填充和填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆