[英]How to display the first 100 characters in a gridview?
I use a gridview to display data, but sometimes the data is to big to be displayed in a cell. 我使用gridview来显示数据,但是有时数据很大,无法显示在单元格中。 Can I use a method to allow the gridview to display fe the first 100 characters of a string? 我可以使用一种方法允许gridview显示字符串的前100个字符吗?
any help is welcome! 欢迎任何帮助!
You can handle the RowDataBound event of the gridview and cut the text length, like so: 您可以处理gridview的RowDataBound事件并削减文本长度,如下所示:
protected void gvNotes_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex < 0)
return;
int _myColumnIndex = 0; // Substitute your value here
string text = e.Row.Cells[_myColumnIndex].Text;
if (text.Length > 100)
{
e.Row.Cells[_myColumnIndex].Text = text.Substring(0, 100);
}
}
Create this function 创建此功能
public object TrimString(string input, int length)
{
// return nothing if the string is null
if (String.IsNullOrEmpty(input))
{
return string.Empty;
}
// invalid length submitted
if (length <= 0)
{
length = 100;
}
if (input.Length > length)
{
return input.Substring(0, length) + "...";
}
return input;
}
And you call it from your aspx page like this. 您可以从aspx页面像这样调用它。
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# TrimString(Eval("CustName").ToString(),100) %>'></asp:Label>
</ItemTemplate>
For an IE only answer, you could use CSS and, as long as you have a set width for the column, set overflow:ellipsis, or overflow:hidden (should work for all browsers), if you don't want the dots. 对于仅适用于IE的答案,可以使用CSS,并且只要您具有设置的列宽,就可以设置一点:省略号或溢出:隐藏(适用于所有浏览器)(如果您不希望使用点)。
OK, as per the comment, I haven't used gridviews for a while, but it would be a matter of setting a CSS class for each of those cells and in the class: 好的,根据评论,我已经有一段时间没有使用gridviews了,但这将是为这些单元格和类中的每个单元格设置CSS类的问题:
trimText
{
overflow:ellipsis;
}
There's also some hacking you can do to get this to display cross browser - some notes here: 您还可以采取一些措施使它显示在跨浏览器中-这里有一些注意事项:
http://www.jide.fr/english/emulate-text-overflowellipsis-in-firefox-with-css http://www.jide.fr/english/emulate-text-overflowellipsis-in-firefox-with-css
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.