Datagrid behaviour on long lines ...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have a datagrid whose columns often contains long words. When such a word
is at the end of the column width, it doesn't display any part of the word
(i.e. removes the word completely). How can I change this? I want the grid to
show the part of the grid that fits, i.e.

This is a very long woooooooooooord

Current behaviour:

This is a very long | nextcolumn

Desired behaviour:

This is a very long wooo | nextcolumn

Thanks.
 
Manual cutting? Before showing data in the datagrid iterate through all
records and implement something like this:

frmColumn = (frmColumn.Length > MaxLength ? frmColumn.Substring(0,
MaxLength) + "..." : frmColumn);

from "abcdefghi"

it will produce:

"abcde..."


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Manual cutting doesn't work as it used to (where a character had a pixed
width), because each letter uses a different number of pixels. So it produces
ugly results...

Calculating the actual width of the string (in pixels) seems very very
expensive to even consider it .

Any other idea you can think of?

Thanks!
 
Well to calculate the actual string in pixels not so hard task - just
call MeasureString but to calculate the maximum visible width for that
string is more harder. The fast solution (the complexity is log2n) is to
use binary search where string + Width that returned by MeasureString
could be a value and passed maximum value is a key. I've written for you
a small example:
http://www.sergeybogdanov.com/Samples/OptimalStrLength.zip


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Sergey,

I was doing that before I read your post. I optimized a bit by assuming that
the minimum number of characters but be column_pixels /
size_of_letter_"W"_in_pixels. Not a huge improvement but everything helps.

Anyway I have a large grid and this calculation is a bit expensive when you
have 1000 rows (now we could argue that if I'm loading 1000 rows at the same
time I'm doing something wrong, but that's another topic).

Thanks!
 
Back
Top