C#DataGridView AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells [英] C# DataGridView AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells

查看:1259
本文介绍了C#DataGridView AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataGridView中显示最多100,000行的表。该表有一列包含大字符串。我发现将AutosizeMode设置为AllCells会导致应用程序在计算所需宽度的同时冻结很长时间。
作为妥协,我将自动调整模式设置为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屋!

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