在gridview中显示数据集单元格的子字符串

时间:2021-04-24 10:35:01

I want to do create a gridview with dataset, I am hiding some columns after databind. And I also need to display Descriptin column's first 50 characters. How can I do that? here is my code

我想用数据集创建一个gridview,我在databind之后隐藏了一些列。而且我还需要显示Descriptin列的前50个字符。我怎样才能做到这一点?这是我的代码

protected void grid_all_posts_DataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false;
    e.Row.Cells[1].Visible = false;

     // I want to display only substring in Gridview
     e.Row.Cells[3].Text = e.Row.Cells[3].Text.ToString().Substring(0,50);
}

I hope it is clear

我希望很清楚

2 个解决方案

#1


2  

Instead of only showing a set number of characters, you might consider using the CSS3 text-overflow property. Using this property, you can specify the max-width in pixels, and show an elipses to indicate that more text is available.

您可以考虑使用CSS3文本溢出属性,而不是仅显示一定数量的字符。使用此属性,您可以指定max-width(以像素为单位),并显示一个elipses以指示可用的文本更多。

<div style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:150px">
    <span title="Put your full text here">
        This is some really long text. We want it to cut off after a specified number of pixels, and show the elipses to indicate that more text is available.
    </span>
</div>

With the above example, you can put the whole text in the tooltip/title, which can be viewed when the user hovers over the text.

通过上面的示例,您可以将整个文本放在工具提示/标题中,当用户将鼠标悬停在文本上时可以查看该文本。

#2


0  

this error is for SubString method. when string length is less than 50 Substring rise exception

此错误适用于SubString方法。当字符串长度小于50子串上升异常时

replace your last line code with this:

用这个替换你的上一行代码:

e.Row.Cells[3].Text = (e.Row.Cells[3].Text.Length>50) ? e.Row.Cells[3].Text.ToString().Substring(0,50) : e.Row.Cells[3].Text;

this Code first check length of string and call substring if necessery.

此代码首先检查字符串的长度,如果需要则调用子字符串。

#1


2  

Instead of only showing a set number of characters, you might consider using the CSS3 text-overflow property. Using this property, you can specify the max-width in pixels, and show an elipses to indicate that more text is available.

您可以考虑使用CSS3文本溢出属性,而不是仅显示一定数量的字符。使用此属性,您可以指定max-width(以像素为单位),并显示一个elipses以指示可用的文本更多。

<div style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:150px">
    <span title="Put your full text here">
        This is some really long text. We want it to cut off after a specified number of pixels, and show the elipses to indicate that more text is available.
    </span>
</div>

With the above example, you can put the whole text in the tooltip/title, which can be viewed when the user hovers over the text.

通过上面的示例,您可以将整个文本放在工具提示/标题中,当用户将鼠标悬停在文本上时可以查看该文本。

#2


0  

this error is for SubString method. when string length is less than 50 Substring rise exception

此错误适用于SubString方法。当字符串长度小于50子串上升异常时

replace your last line code with this:

用这个替换你的上一行代码:

e.Row.Cells[3].Text = (e.Row.Cells[3].Text.Length>50) ? e.Row.Cells[3].Text.ToString().Substring(0,50) : e.Row.Cells[3].Text;

this Code first check length of string and call substring if necessery.

此代码首先检查字符串的长度,如果需要则调用子字符串。