C#DataGridView AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells [英] C# DataGridView AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
问题描述
作为妥协,我将自动调整模式设置为DisplayedCells。
然后我将一个方法绑定到dataGrid的滚动事件:
public void MethodThatBindsDataToTheDatagridview(DataTable table)
{
dataGrid.Source = table;
dataGrid.Columns [1] .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dataGrid.Columns [2] .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
}
pubic void DataGridScroll(object sender,ScrollEventArgs e)
{
((DataGridView)sender).Update();
}
我也尝试过与Refresh方法相同的。我的期望是DataGrid将根据显示的行设置列的宽度。但是,当加载表时,这只会发生一次,但滚动事件不会触发列宽度的更改。
调用 AutoResizeColumn 方法datagridview是你需要做的:
dataGrid.AutoResizeColumn(1,DataGridViewAutoSizeColumnMode.DisplayedCells);
dataGrid.AutoResizeColumn(2,DataGridViewAutoSizeColumnMode.DisplayedCells);
I am displaying a table with up to 100,000 rows in a DataGridView. The table has one column which contains large strings. I found out that setting the AutosizeMode to 'AllCells' in causes the application to freeze for a long time while its computing the required width. As a compromise, I set the Autosize mode to DisplayedCells. I then bound a method to the dataGrid's scroll event:
public void MethodThatBindsDataToTheDatagridview(DataTable table)
{
dataGrid.Source = table;
dataGrid.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dataGrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
}
pubic void DataGridScroll(object sender, ScrollEventArgs e)
{
((DataGridView)sender).Update();
}
I also tried the same with Refresh method. My expectation is that the DataGrid will set the columns width according to the displayed rows. However, this happens only once when the table is loaded, but the scroll event does not trigger a change in the columns width.
Calling the AutoResizeColumn method on the datagridview is what you need to do:
dataGrid.AutoResizeColumn(1, DataGridViewAutoSizeColumnMode.DisplayedCells);
dataGrid.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.DisplayedCells);
这篇关于C#DataGridView AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!