DataGrid 某一列如果字数太多 就会往旁边撑开来以便显示全部字数~!能不能 超出字数以后用...代替呢
解决方案 »
- 值不能为空。参数名: path System.ArgumentNullException: 值不能为空。
- Web.config中的,compilation 真假问题?
- datagrid控件快捷键功能
- 在线考试问题,百分求解
- 请问<asp.net 第一步-基于c#和asp.net 2.0>那里有原代码下载
- asp.net中点击input,然后转到别的页面,怎么写?
- 请教在where子语句中的session该怎么写?
- 初学ASP.NET,新建一个工程,输出Hello World!报错,郁闷!
- 两DropDownList用AJAX联动
- 我给一个网站实现发邮件,发群组邮件有别的什么技术吗?
- Socket编程高手进来
- 不喜欢用控件的牛人进
你可以西安搜索一下帖子再说
public const string ConstChineseRegex = @"^[\u4e00-\u9fa5]+$";
public const string ConstEnglishRegex = @"^[a-zA-Z_0-9]+$";
public const string ConstChineseEnglishRegex = @"^[\u4e00-\u9fa5\w]+$";
public static bool IsChinese(string text)
{ return System.Text.RegularExpressions.Regex.Match(text, ConstChineseRegex, System.Text.RegularExpressions.RegexOptions.Compiled).Success;
} public static bool IsChinese(char ch)
{
return IsChinese(Convert.ToString(ch));
} public static bool IsEnglish(string text)
{ return System.Text.RegularExpressions.Regex.Match(text, ConstEnglishRegex, System.Text.RegularExpressions.RegexOptions.Compiled).Success;
} public static bool IsEnglish(char ch)
{
return IsEnglish(Convert.ToString(ch));
} public static bool IsChineseEnglish(string text)
{ return System.Text.RegularExpressions.Regex.Match(text, ConstChineseEnglishRegex, System.Text.RegularExpressions.RegexOptions.Compiled).Success;
} public static bool IsChineseEnglish(char ch)
{
return IsChineseEnglish(Convert.ToString(ch));
}
public static int GetLength(string str)
{
int length = 0; int countChinese = 0;
int countEnglish = 0;
for (int i = 0; i < str.Length; i++)
{
char ch = str[i]; if (RegexHelper.IsEnglish(ch))
{
countEnglish++;
}
else if (RegexHelper.IsChinese(ch))
{
countChinese++;
}
} length = countChinese * 2 + countEnglish; return length;
} public static string SubStringReturnSameWidth(string str, int length, string endStr)
{
if (string.IsNullOrEmpty(str))
{
return null;
}
if (string.IsNullOrEmpty(endStr))
{
endStr = string.Empty;
}
string temp = str.Substring(0, (str.Length < length + 1) ? str.Length : length + 1);
byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp); string outputStr = "";
int count = 0; for (int i = 0; i < temp.Length; i++)
{
if ((int)encodedBytes[i] == 63)
count += 2;
else
count += 1; if (count <= length - endStr.Length)
outputStr += temp.Substring(i, 1);
else if (count > length)
break;
} if (count <= length)
{
outputStr = temp;
endStr = "";
} outputStr += endStr; return outputStr;
}调用示例string result=SubStringReturnSameWidth("2008奥运OK!",6,"…");//中文算2
显示:2008奥运
for(int i = 0; i<ds.table[0].rows.count;i++)
{
string address =ds.table[0].rows[0]["address"].tostring();
if(address.length>20)
{
address = address.Substring(0, 20) + "...";
ds.Tables[0].Rows[i]["address"] = address;
}
}
最后填充,然后绑定!
/// <summary>
/// 截取字符串(适用于中英文混合)
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="len">长度</param>
/// <returns></returns>
public static string CutString(string str, int len)
{
str = str.Trim();
byte[] myByte = System.Text.Encoding.Default.GetBytes(str);
if (myByte.Length > len)
{
string result = "";
for (int i = 0; i < str.Length; i++)
{
byte[] tempByte = System.Text.Encoding.Default.GetBytes(result);
if (tempByte.Length < len)
{
result += str.Substring(i, 1);
}
else
{
break;
}
}
return result + "...";
}
else
{
return str;
}
}
试试这个