国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 楚雄市| 芜湖市| 历史| 防城港市| 门源| 富裕县| 崇文区| 威远县| 伊宁市| 普兰店市| 阳新县| 扎鲁特旗| 桐柏县| 兴文县| 新郑市| 巫山县| 炎陵县| 锡林郭勒盟| 武定县| 芜湖市| 罗平县| 余干县| 运城市| 岑溪市| 海兴县| 丰顺县| 太原市| 九台市| 信丰县| 洪雅县| 四会市| 渝北区| 芒康县| 林芝县| 凉城县| 抚顺市| 蒙城县| 嵩明县| 大宁县| 中牟县| 鄢陵县|