G
Guest
I am trying to make a method that will autosize columns to fit the longest
text contained in a DataGrid.
My plan was to take the width of an arbitrary character (underscore, in this
case) and multiply this width to the longest # of characters found for each
column.
But it comes out wrong... I find the longest string no problem, but when I
multiply it by the width that I measured for a single character, it comes out
too big... the columns in the datagrid then get stretched out too wide,
instead of just snug like I wanted. What gives?
Font font = datagrid.Font;
float fontWidth = g.MeasureString("_", font).Width;
// find longest strings in each of datagrid's columns
....
// change column style's width to product of longest string length and font
width
styles[x].Width = (int)(max_widths[x] * fontWidth);
text contained in a DataGrid.
My plan was to take the width of an arbitrary character (underscore, in this
case) and multiply this width to the longest # of characters found for each
column.
But it comes out wrong... I find the longest string no problem, but when I
multiply it by the width that I measured for a single character, it comes out
too big... the columns in the datagrid then get stretched out too wide,
instead of just snug like I wanted. What gives?
Font font = datagrid.Font;
float fontWidth = g.MeasureString("_", font).Width;
// find longest strings in each of datagrid's columns
....
// change column style's width to product of longest string length and font
width
styles[x].Width = (int)(max_widths[x] * fontWidth);