如果你有一个实体绑定到你的gridview,你可以这样做:
首先,添加一个新属性:
[NotMapped]
public string CutDescription
{
get
{
if (Description.Length <= 1000)
{
return Description;
}
return Description.Substring(0, 1000) + "...";
}
}
然后你可以将它绑定到你的gridview:
<asp:BoundField DataField="CutDescription" HeaderText="Description" />
这只是一种方法。希望能帮助到你。
编辑:使用 RowDatabound 事件的另一种方式:
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
var row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
// Just change the index of the cell
var description = row.Cells[1].Text;
if (description.Length > 100)
{
row.Cells[1].Text = description.Substring(0, 100) + "...";
}
}
}