[英]c# datagridview column autosizemode
I wish that by default the columns uses the 我希望默认情况下列使用
AutoSizeMode = DisplayedCells;
but I wish also the possibility to resize the columns, but DisplayedCells
type doesn't allow to resize.. 但我希望也可以调整列的大小,但
DisplayedCells
类型不允许调整大小..
any ideas? 有任何想法吗?
You can call the sub DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
whenever it's convenient, such as after you've loaded the data. 只要方便,您可以调用子
DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
,例如在加载数据之后。 Leave the DataGridView.AutoSizeColumnsMode
property alone and the user will still be able to resize the columns themselves, but they'll have a comfortable start. 保留
DataGridView.AutoSizeColumnsMode
属性,用户仍然可以自己调整列的大小,但它们将有一个舒适的开始。 Best of both worlds. 两全其美。
Row: 行:
dataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
Column: 柱:
dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
I don't think you can achieve that because the AutoSizeMode once is set to DisplayedCells all the behavior is controlled by design. 我不认为你可以实现这一点,因为AutoSizeMode一旦设置为DisplayedCells,所有行为都由设计控制。 But I have this idea.
但我有这个想法。 You should have to keep your column (I suppose Columns[0] for demonstrative purpose) AutoSizeMode fixed at
DataGridViewAutoSizeColumnMode.None
. 你应该保留你的列(我认为Columns [0]用于演示目的)AutoSizeMode固定在
DataGridViewAutoSizeColumnMode.None
。 You want to set it as DisplayedCells
because you may want the column width to expand or collapse depending on the Cell text length. 您希望将其设置为
DisplayedCells
因为您可能希望根据单元格文本长度扩展或折叠列宽。 So my idea is every time the CellBeginEdit
starts, we set the AutoSizeMode
to DisplayedCells and when the CellEndEdit
starts we save the Width (which is autosized for you) before reseting the AutoSizeMode
to None
, then assign the column Width to that saved value. 所以我的想法是每次
CellBeginEdit
启动时,我们将AutoSizeMode
设置为DisplayedCells,当CellEndEdit
启动时,我们保存宽度(为您自动调整),然后将AutoSizeMode
为None
,然后将列Width分配给该保存的值。 Here is my code: 这是我的代码:
//First before loading data
private void form_Load(object sender, EventArgs e){
dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
//Fill your dataGridView here
//.........
//.........
int w = dataGridView.Columns[0].Width;
//reset to None
dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView.Columns[0].Width = w;
}
//Now for CellBeginEdit and CellEndEdit
private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if(e.ColumnIndex == 0) //because I suppose the interested column here is Columns[0]
dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
}
private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == 0){
int w = dataGridView.Columns[0].Width;
dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView.Columns[0].Width = w;
}
}
I tested the code and it seems to work OK, there is a case that it won't work because we don't add code for that case, that is when the Cell value is changed by code. 我测试了代码,它似乎工作正常,有一种情况它不会工作,因为我们不为这种情况添加代码,即通过代码更改Cell值。
I have to say that your want is a little strange, I don't care too much about the column's width, user should know how to do with it. 我不得不说你想要的有点奇怪,我不太关心列的宽度,用户应该知道如何处理它。
In one of my applications, I have autosize set to displayedcells. 在我的一个应用程序中,我将自动调整设置为显示的单元格。 Then once the form is loaded, I turn off autosize in order to allow the users to do the sizing.
然后,一旦加载表单,我关闭自动调整大小,以允许用户进行大小调整。
private void Form1_Load(object sender, EventArgs e)
{
// Designer has autosize set to displayedcells.
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; // Turn off autosize
dataGridView1.AllowUserToResizeRows = true; // Turn on letting user size columns
dataGridView1.AllowUserToOrderColumns = true;
}
The only thing that worked for me in Visual Studio 2008 (and VB.net) was: 在Visual Studio 2008(和VB.net)中唯一对我有用的是:
For i As Integer = 0 To grdList2.Columns.Count - 1
If i <> (grdList2.Columns.Count - 1) Then
grdList2.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
Else
grdList2.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End If
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.