@@ -356,6 +356,7 @@ private void queryWorker_RunWorkerCompleted(object sender, System.ComponentModel
356
356
}
357
357
358
358
grid . DataSource = arg . Results ;
359
+ AutoResizeColumns ( grid ) ;
359
360
}
360
361
361
362
queryStatusBar1 . UpdateValues ( arg . Results . Rows . Count , arg . QueryTime , ( long ? ) arg . Results . ExtendedProperties [ "TotalRows" ] ) ;
@@ -416,6 +417,48 @@ from error in arg.Errors
416
417
}
417
418
}
418
419
420
+ private static void AutoResizeColumns ( DataGridView grid )
421
+ {
422
+ const int maxSize = 200 ;
423
+ const int widthFudgeFactor = 25 ;
424
+
425
+ int [ ] preferredSizes = new int [ grid . ColumnCount ] ;
426
+ int excessPreferredSize = 0 ;
427
+ int totalCurrentColumnSize = 0 ;
428
+ grid . AutoResizeColumns ( DataGridViewAutoSizeColumnsMode . AllCells ) ;
429
+ for ( int i = 0 ; i < grid . ColumnCount ; i ++ )
430
+ {
431
+ DataGridViewColumn column = grid . Columns [ i ] ;
432
+ preferredSizes [ i ] = column . Width ;
433
+ if ( column . Width > maxSize )
434
+ {
435
+ excessPreferredSize += column . Width - maxSize ;
436
+ column . Width = maxSize ;
437
+ }
438
+ totalCurrentColumnSize += column . Width ;
439
+ }
440
+
441
+ if ( excessPreferredSize > 0 )
442
+ {
443
+ // Some columns would like to be larger. Can we do this without incurring horizontal scroll?
444
+ // Easiest to just always leave room for a vertical scroll bar
445
+ int availableWidth = grid . DisplayRectangle . Width - totalCurrentColumnSize
446
+ - SystemInformation . VerticalScrollBarWidth - widthFudgeFactor ;
447
+ if ( availableWidth > 0 )
448
+ {
449
+ double grantRatio = Math . Min ( 1.0 , ( double ) availableWidth / excessPreferredSize ) ;
450
+ for ( int i = 0 ; i < grid . ColumnCount ; i ++ )
451
+ {
452
+ DataGridViewColumn column = grid . Columns [ i ] ;
453
+ if ( column . Width != preferredSizes [ i ] )
454
+ {
455
+ column . Width += ( int ) ( ( preferredSizes [ i ] - column . Width ) * grantRatio ) ;
456
+ }
457
+ }
458
+ }
459
+ }
460
+ }
461
+
419
462
private void ShowLog ( string lastReplyLog )
420
463
{
421
464
logTextbox . Text = lastReplyLog ?? string . Empty ;
0 commit comments