C# DataGridView 列自动调整宽度和可调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17726298/
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 column auto adjust width and resizable
提问by wallou
I am wondering if it is possible to have a dataGridView column to adjust automatically its width to its content and at the same time being resizable by the user ?
我想知道是否有可能让 dataGridView 列根据其内容自动调整其宽度,同时由用户调整大小?
Here what i have tried so far :
这是我迄今为止尝试过的:
dataGridView.AllowUserToResizeColumns = true;
dataGridView.Columns[0].AutoSizeMode=DataGridViewAutoSizeColumnMode.DisplayedCells;
dataGridView.Columns[0].Resizable = DataGridViewTriState.True;
But, I am still unable to resize the column size manually.
但是,我仍然无法手动调整列大小。
If anyone already faced this issue or have any idea let me know.
如果有人已经遇到过这个问题或有任何想法,请告诉我。
Thanks.
谢谢。
采纳答案by wallou
I finally find a way to do what I wanted.
我终于找到了做我想做的事情的方法。
The idea is to
这个想法是
- let the
dataGridView
resize the columns itself to fit the content, and then - change the
AutoSizeColumnMode
and set the width with the value you just stored.
- 让
dataGridView
调整列本身的大小以适应内容,然后 - 更改
AutoSizeColumnMode
并使用您刚刚存储的值设置宽度。
Here is the code:
这是代码:
dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
int widthCol = dataGridView.Columns[i].Width;
dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView.Columns[i].Width = widthCol;
Hope this will help.
希望这会有所帮助。
回答by Justin
Building on the code above to iterate it for all columnsand also auto adjust the width of the DataGridView
以上面的代码为基础,对所有列进行迭代,并自动调整列的宽度DataGridView
//initiate a counter
int totalWidth = 0;
//Auto Resize the columns to fit the data
foreach (DataGridViewColumn column in mydataGridView.Columns)
{
mydataGridView.Columns[column.Index].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
int widthCol = mydataGridView.Columns[column.Index].Width;
mydataGridView.Columns[column.Index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
mydataGridView.Columns[column.Index].Width = widthCol;
totalWidth = totalWidth + widthCol;
}
//the selector on the left of the DataGridView is about 45 in width
mydataGridView.Width = totalWidth + 45;
回答by user34660
The thing that is not obvious (it was not to me) is that use of AutoSizeMode means we want the size to be managed, so that implies that the user would not (cannot) do a resize. So what I have tried and it seems to work is to use DataGridView.AutoResizeColumn (note that it does a one-time resize) for each column or most columns then set DataGridView.AllowUserToResizeColumns to true. There are probably other variations but the important thing is that use of AutoSizeMode is mutually exclusive with allowing the user to do a resize.
不明显的事情(对我来说不是)是使用 AutoSizeMode 意味着我们希望管理大小,因此这意味着用户不会(不能)调整大小。所以我尝试过并且似乎有效的是对每一列或大多数列使用 DataGridView.AutoResizeColumn(注意它会一次调整大小),然后将 DataGridView.AllowUserToResizeColumns 设置为 true。可能还有其他变化,但重要的是 AutoSizeMode 的使用与允许用户调整大小是相互排斥的。
回答by user1779049
- Thanks for the solution above (To iterate through the
DataGridView.Columns
, changeAutoSizeMode
to a valid one, collect width value and set it back after changeAutoSizeMode
toDataGridViewAutoSizeColumnMode.None
). - I struggled with it, and noticed it won't work whenever it is called from the class constructor or any line before
Form.Show()
orForm.ShowDialog()
. So I put this code snippet in theForm.Shown
event and this works for me. My transformed code, reguardless of whatever
DataGridView.AutoSizeColumnsMode
set before, I useDataGridViewColumn.GetPreferredWidth()
instead of changingDataGridViewColumn.AutoSizeMode
and set the width value immediately, then changeDataGridView.AutoSizeColumnsMode
once:private void form_Shown(object sender, EventArgs e) { foreach (DataGridViewColumn c in dataGridView.Columns) c.Width = c.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, true); dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; }
Be sure to set
dataGridView.AllowUserToResizeColumns = true;
I don't know how come this only works after the form is shown.
- 感谢上述解决方案(要遍历
DataGridView.Columns
,更改AutoSizeMode
为有效值,收集宽度值并在更改AutoSizeMode
为后将其重新设置DataGridViewAutoSizeColumnMode.None
)。 - 我与它挣扎着,并注意到每当它是从类的构造函数或之前的任何行调用它不会工作
Form.Show()
或Form.ShowDialog()
。所以我把这个代码片段放在Form.Shown
事件中,这对我有用。 我转换后的代码,无论
DataGridView.AutoSizeColumnsMode
之前设置什么,我都使用DataGridViewColumn.GetPreferredWidth()
而不是DataGridViewColumn.AutoSizeMode
立即更改和设置宽度值,然后更改DataGridView.AutoSizeColumnsMode
一次:private void form_Shown(object sender, EventArgs e) { foreach (DataGridViewColumn c in dataGridView.Columns) c.Width = c.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, true); dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; }
一定要设置
dataGridView.AllowUserToResizeColumns = true;
我不知道这仅在显示表单后才有效。
回答by asady
You can just use this simple code:
你可以使用这个简单的代码:
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
After setting datasource. It will set the width and allow resize.
设置数据源后。它将设置宽度并允许调整大小。
回答by Sheenit Mathew
Simple solution: Either bound filed or template filed. you can use ItemStyle-Width
简单的解决方案:绑定归档或模板归档。您可以使用ItemStyle-Width
<asp:BoundField DataField="SSSS" HeaderText="XXX" ItemStyle-Width="125px"/>
<asp:TemplateField HeaderText="EEEE" ItemStyle-Width="100px">