1. This Code is the subject that processes long strings. Void itemdatabound (Object sender, datagriditemeventargs E)
{
// Get the string to be displayed
String title = getthestring ();
// Returns the updated text for the specified Column
String newtext = adjusttextfordisplay (title, 1, grid );
// Set the text including the tooltip when necessary
E. Item. cells [1]. Text = newtext;
}
2. The adjusttextfordisplay (string, Int, DataGrid) function intercepts long strings Based on the column width;
Note that the font and columns [colindex]. itemstyle. Width attributes of the DataGrid must be assigned values. If no value is assigned, the function uses the default value. If no processing is added, an exception occurs in the function.
String adjusttextfordisplay (string text, int colindex, DataGrid grid)
{
// Calculate the dimensions of the text with the current font
Sizef textsize = measurestring (text, grid. font );
// Compare the size with the column's width
Int colwidth = (INT) grid. Columns [colindex]. itemstyle. Width. value;
If (textsize. width> colwidth)
{
// Get the exceeding pixels
Int Delta = (INT) (textsize. Width-colwidth );
// Calculate the average width of the characters (approx)
Int avgcharwidth = (INT) (textsize. width/text. Length );
// Calculate the number of chars to trim to stay in the fixed width (approx)
Int chrtotrim = (INT) (delta/avgcharwidth );
// Get the proper substring + The ellipsis
// Trim 2 more Chars (approx) to make room for the ellipsis
String rawtext = text. substring (0, text. Length-(chrtotrim + 2) + "";
// Format to add a tooltip
String FMt = "{1 }";
Return string. Format (FMT, text, rawtext );
}
Return text;
}