I am showing a table with up to 100,000 rows in a DataGridView. The table has one column that contains large rows. I found out that setting AutosizeMode to "AllCells" causes the application to freeze for a long time while calculating the required width. As a compromise, I set Autosize mode to DisplayedCells. Then I attached the method to the dataGrid 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 the Refresh method. I expect the DataGrid to set the width of the columns according to the rows displayed. However, this only happens once when the table is loaded, but the scroll event does not cause the column width to change.
source share