C# DataGridView 自动调整和填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18666582/
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 AutoFit and Fill
提问by James Jeffery
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.
我的DataGridView
. 我想要做的是让前 2 列自动适应内容的宽度,并让第 3 列填充剩余空间。
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.
可以在 WinForms 中做吗?如果有任何用处,我正在从 EF DataContext 加载我的数据。我已经包含了它当前外观的图像。
采纳答案by Chris
You need to use the DataGridViewColumn.AutoSizeMode
property.
您需要使用该DataGridViewColumn.AutoSizeMode
属性。
You can use one of these values for column 0 and 1:
您可以对第 0 列和第 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.
AllCells:调整列宽以适应列中所有单元格的内容,包括标题单元格。
AllCellsExceptHeader:调整列宽以适应列中所有单元格的内容,不包括标题单元格。
DisplayedCells:调整列宽以适应当前显示在屏幕上的行中的列中所有单元格的内容,包括标题单元格。
DisplayedCellsExceptHeader:调整列宽以适应当前显示在屏幕上的行中列中所有单元格的内容,不包括标题单元格。
Then you use the Fillvalue for column 2
然后您使用第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.
正如其他用户所指出的,默认值可以在属性datagridview
级别设置DataGridView.AutoSizeColumnsMode
。
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 AFTERcolumns have been created.
如果您的网格绑定到数据源并且列是自动生成的(AutoGenerateColumns
属性设置为 True),您需要使用该DataBindingComplete
事件在创建列后应用样式。
In some scenarios (change cells value by code for example), I had to call DataGridView1.AutoResizeColumns();
to refresh the grid.
在某些情况下(例如通过代码更改单元格值),我不得不调用DataGridView1.AutoResizeColumns();
以刷新网格。
回答by Sriram Sakthivel
Not tested but you can give a try.Tested and working. I hope you can play with AutoSizeMode
of DataGridViewColum
to achieve what you need.
未测试,但您可以尝试。测试和工作。我希望您可以使用AutoSizeMode
ofDataGridViewColum
来实现您的需要。
Try setting
尝试设置
dataGridView1.DataSource = yourdatasource;<--set datasource before you set AutoSizeMode
//Set the following properties after setting datasource
dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
This should work
这应该工作
回答by Durga Prasad Guntoju
public void setHeight(DataGridView src)
{
src.Height= src.ColumnHeadersVisible ? src.ColumnHeadersHeight : 0 + src.Rows.OfType<DataGridViewRow>().Where(row => row.Visible).Sum(row => row.Height);
}
回答by Aki
Try doing,
尝试做,
AutoSizeColumnMode = Fill;
回答by Jay
public static void Fill(DataGridView dgv2)
{
try
{
dgv = dgv2;
foreach (DataGridViewColumn GridCol in dgv.Columns)
{
for (int j = 0; j < GridCol.DataGridView.ColumnCount; j++)
{
GridCol.DataGridView.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
GridCol.DataGridView.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
GridCol.DataGridView.Columns[j].FillWeight = 1;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
回答by Ahosan Karim Asik
Just change Property from property of control:AutoSizeColumnsMode:Fill
只需从控制属性更改属性:AutoSizeColumnsMode:Fill
OR By code
或按代码
dataGridView1.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;
回答by AlfredBr
This is my favorite approach...
这是我最喜欢的方法...
_dataGrid.DataBindingComplete += (o, _) =>
{
var dataGridView = o as DataGridView;
if (dataGridView != null)
{
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView.Columns[dataGridView.ColumnCount-1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
};
回答by Sudhir
Try this :
尝试这个 :
DGV.AutoResizeColumns();
DGV.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.AllCells;
回答by RooiWillie
To build on AlfredBr's answer, if you hid some of your columns, you can use the following to auto-size all columns and then just have the last visible column fill the empty space:
以 AlfredBr 的回答为基础,如果您隐藏了一些列,则可以使用以下命令自动调整所有列的大小,然后让最后一个可见列填充空白空间:
myDgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
myDgv.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.None).AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
回答by Ignacio
This is what I have done in order to get the column "first_name" fill the space when all the columns cannot do it.
这是我所做的,以便在所有列都无法完成时让“first_name”列填充空间。
When the grid go to small the column "first_name" gets almost invisible (very thin) so I can set the DataGridViewAutoSizeColumnMode to AllCells as the others visible columns. For performance issues it′s important to set them to None before data binding it and set back to AllCell in the DataBindingComplete event handler of the grid. Hope it helps!
当网格变小时,列“first_name”几乎不可见(非常细),因此我可以将 DataGridViewAutoSizeColumnMode 设置为 AllCells 作为其他可见列。对于性能问题,重要的是在数据绑定之前将它们设置为 None 并在网格的 DataBindingComplete 事件处理程序中设置回 AllCell。希望能帮助到你!
private void dataGridView1_Resize(object sender, EventArgs e)
{
int ColumnsWidth = 0;
foreach(DataGridViewColumn col in dataGridView1.Columns)
{
if (col.Visible) ColumnsWidth += col.Width;
}
if (ColumnsWidth <dataGridView1.Width)
{
dataGridView1.Columns["first_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
else if (dataGridView1.Columns["first_name"].Width < 10) dataGridView1.Columns["first_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}