[IRD] How to intercept long strings

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.