国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

[DataGird]如何截取過長的字符串

2024-07-21 02:29:34
字體:
來源:轉載
供稿:網友

  1.這段代碼是處理過長字符串的主體;

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.adjusttextfordisplay(string,int,datagrid)函數的功能是根據列的寬度,截取過長的字符串;這里需要注意的是datagrid的font和columns[colindex].itemstyle.width屬性必需有賦值。如果沒有賦值的話,函數將會采用系統默認的值。如不加處理,函數會出異常。

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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上栗县| 临沧市| 泽库县| 当阳市| 泾川县| 桐庐县| 酒泉市| 安龙县| 新野县| 双柏县| 芜湖市| 蛟河市| 孟津县| 云霄县| 正定县| 宜丰县| 荣成市| 惠州市| 湖北省| 福海县| 刚察县| 白河县| 互助| 衡阳县| 航空| 怀来县| 金寨县| 和龙市| 富宁县| 资源县| 甘孜县| 砚山县| 栾城县| 资兴市| 枣强县| 依安县| 青冈县| 邯郸市| 兴义市| 鄂伦春自治旗| 嘉黎县|