问个gridview控制列的文字显示个数的问题.
我想把gridview中一个显示文字的列控制只显示三个字,请问有什么好办法.
------解决方案-------------------- <ItemTemplate>
<asp:LinkButton id= "lbtnMessage " runat= "server " Text= ' <%# CheckString(DataBinder.Eval(Container.DataItem, "MessageAnswer ").ToString()) %> ' CausesValidation= "False " CommandName= "Edit ">
</asp:LinkButton>
</ItemTemplate>
/// <summary>
/// 功能描述:转换DATAGRID列显示的字符串,去掉文本样式,并控制长度 --Add 韩国际 2006-6-26
/// </summary>
/// <param name= "strMessageAnswer "> 传入的系统消息内容 </param>
/// <returns> 返回处理过的系统消息内容 </returns>
protected string CheckString(string strMessageAnswer)
{
if(strMessageAnswer.Trim()!= " ")
{
//去掉所有样式
strMessageAnswer = Regex.Replace(strMessageAnswer.Trim(), " <.+?> ", " ");
if(strMessageAnswer.Length> 20)
{
strMessageAnswer = 截取字符串
}
}
else
{
strMessageAnswer = "无内容 ";
}
return strMessageAnswer;
}
------解决方案--------------------你自己定义一个textcloumn
然后添加到gridview中
自定义该textcloumn.length=3;
------解决方案-------------------- <ItemTemplate>
<asp:Label ID= "Label1 " runat= "server "
Text= ' <%# String.Format( "{0} ", Eval( "mainbbs_maintitle ")).Length > = 3 ? String.Format( "{0} ", Eval( "mainbbs_maintitle ")).Substring(0, 3) + ".. " : String.Format( "{0} ", Eval( "mainbbs_maintitle ")).Substring(0) + ".. "%> '> </asp:Label>
</ItemTemplate>
绑定的数据源字段
LZ自己悟吧
个人见解,共同学习
------解决方案--------------------用sql控制.
select left(Title,10) as title from table
------解决方案--------------------/// <summary>
/// 截取指定长度字符并在其后添加指定字符
/// </summary>
/// <param name= "str "> 源字符串 </param>
/// <param name= "ilength "> 指定长度 </param>
/// <param name= "append "> 添加字符串 </param>
/// <returns> </returns>
public static string CutString(string str, int ilength, string append)
{
char[] cha = str.ToCharArray();
string result = " ";
for (int i = 0; i < ilength; i++)
{
if (i > = str.Length)
{
return result;
}
else
{
result += cha[i];
}
}
result += append;
return result;
}
调用示例:
CutString( "绑定列 ", 3, " ");