@@ -971,14 +971,45 @@ internal int[] ComputeColumnWidths(int availableWidth, List<TableColumn> cols, L
971971 }
972972 else if ( totalNatural > contentWidth )
973973 {
974- double ratio = ( double ) contentWidth / totalNatural ;
975- int assigned = 0 ;
976- for ( int c = 0 ; c < colCount - 1 ; c ++ )
974+ // Shrink only auto-width columns first; preserve fixed-width columns
975+ int fixedTotal = 0 ;
976+ int autoTotal = 0 ;
977+ for ( int c = 0 ; c < colCount ; c ++ )
978+ {
979+ if ( cols [ c ] . Width . HasValue )
980+ fixedTotal += widths [ c ] ;
981+ else
982+ autoTotal += widths [ c ] ;
983+ }
984+
985+ int autoTarget = contentWidth - fixedTotal ;
986+ if ( autoTarget > 0 && autoTotal > 0 )
977987 {
978- widths [ c ] = Math . Max ( 1 , ( int ) ( widths [ c ] * ratio ) ) ;
979- assigned += widths [ c ] ;
988+ double ratio = ( double ) autoTarget / autoTotal ;
989+ int assigned = fixedTotal ;
990+ int lastAutoCol = - 1 ;
991+ for ( int c = 0 ; c < colCount ; c ++ )
992+ {
993+ if ( cols [ c ] . Width . HasValue ) continue ;
994+ widths [ c ] = Math . Max ( 1 , ( int ) ( widths [ c ] * ratio ) ) ;
995+ assigned += widths [ c ] ;
996+ lastAutoCol = c ;
997+ }
998+ if ( lastAutoCol >= 0 )
999+ widths [ lastAutoCol ] = Math . Max ( 1 , widths [ lastAutoCol ] + ( contentWidth - assigned ) ) ;
1000+ }
1001+ else
1002+ {
1003+ // Not enough space even for fixed columns — shrink everything
1004+ double ratio = ( double ) contentWidth / totalNatural ;
1005+ int assigned = 0 ;
1006+ for ( int c = 0 ; c < colCount - 1 ; c ++ )
1007+ {
1008+ widths [ c ] = Math . Max ( 1 , ( int ) ( widths [ c ] * ratio ) ) ;
1009+ assigned += widths [ c ] ;
1010+ }
1011+ widths [ colCount - 1 ] = Math . Max ( 1 , contentWidth - assigned ) ;
9801012 }
981- widths [ colCount - 1 ] = Math . Max ( 1 , contentWidth - assigned ) ;
9821013 }
9831014
9841015 // Cache results
@@ -1046,23 +1077,56 @@ internal int[] ComputeColumnWidthsFromDataSource(int availableWidth, int scrollO
10461077
10471078 for ( int c = 0 ; c < colCount ; c ++ )
10481079 {
1049- int ? colWidth = _dataSource . GetColumnWidth ( c ) ;
1050- bool isAutoCol = ! colWidth . HasValue ;
1080+ int ? dsColWidth = _dataSource . GetColumnWidth ( c ) ;
1081+ bool isAutoCol = ! dsColWidth . HasValue && ! _columnWidthOverrides . ContainsKey ( c ) ;
10511082 if ( autoCount > 0 && ! isAutoCol ) continue ;
10521083 widths [ c ] += perCol ;
10531084 if ( extraCols > 0 ) { widths [ c ] ++ ; extraCols -- ; }
10541085 }
10551086 }
10561087 else if ( totalNatural > contentWidth )
10571088 {
1058- double ratio = ( double ) contentWidth / totalNatural ;
1059- int assigned = 0 ;
1060- for ( int c = 0 ; c < colCount - 1 ; c ++ )
1089+ // Shrink only auto-width columns first; preserve fixed/overridden columns
1090+ int fixedTotal = 0 ;
1091+ int autoTotal = 0 ;
1092+ for ( int c = 0 ; c < colCount ; c ++ )
1093+ {
1094+ bool isFixed = _columnWidthOverrides . ContainsKey ( c ) || _dataSource . GetColumnWidth ( c ) . HasValue ;
1095+ if ( isFixed )
1096+ fixedTotal += widths [ c ] ;
1097+ else
1098+ autoTotal += widths [ c ] ;
1099+ }
1100+
1101+ int autoTarget = contentWidth - fixedTotal ;
1102+ if ( autoTarget > 0 && autoTotal > 0 )
10611103 {
1062- widths [ c ] = Math . Max ( 1 , ( int ) ( widths [ c ] * ratio ) ) ;
1063- assigned += widths [ c ] ;
1104+ double ratio = ( double ) autoTarget / autoTotal ;
1105+ int assigned = fixedTotal ;
1106+ int lastAutoCol = - 1 ;
1107+ for ( int c = 0 ; c < colCount ; c ++ )
1108+ {
1109+ bool isFixed = _columnWidthOverrides . ContainsKey ( c ) || _dataSource . GetColumnWidth ( c ) . HasValue ;
1110+ if ( isFixed ) continue ;
1111+ widths [ c ] = Math . Max ( 1 , ( int ) ( widths [ c ] * ratio ) ) ;
1112+ assigned += widths [ c ] ;
1113+ lastAutoCol = c ;
1114+ }
1115+ if ( lastAutoCol >= 0 )
1116+ widths [ lastAutoCol ] = Math . Max ( 1 , widths [ lastAutoCol ] + ( contentWidth - assigned ) ) ;
1117+ }
1118+ else
1119+ {
1120+ // Not enough space even for fixed columns — shrink everything
1121+ double ratio = ( double ) contentWidth / totalNatural ;
1122+ int assigned = 0 ;
1123+ for ( int c = 0 ; c < colCount - 1 ; c ++ )
1124+ {
1125+ widths [ c ] = Math . Max ( 1 , ( int ) ( widths [ c ] * ratio ) ) ;
1126+ assigned += widths [ c ] ;
1127+ }
1128+ widths [ colCount - 1 ] = Math . Max ( 1 , contentWidth - assigned ) ;
10641129 }
1065- widths [ colCount - 1 ] = Math . Max ( 1 , contentWidth - assigned ) ;
10661130 }
10671131
10681132 return widths ;
0 commit comments