【问题标题】:how to bind substring into gridview and如何将子字符串绑定到gridview和
【发布时间】:2013-06-27 10:07:41
【问题描述】:

我正在使用 gridview 来显示我的 db 表数据。但是有一个名为“描述”的列,包含 1000 个字符。我不想将整个字符串绑定到gridview中。我如何仅将前 100 个字符绑定到 gridview 列???

当我点击选择按钮时,我想弹出带有所选列详细信息的窗口。此时我想显示描述列的所有字符

我已经已经创建了弹出窗口和其他东西。但我仍然无法获得“描述列”的前 100 个字母并将其绑定到 gridview 中。我怎样才能做到这一点 ?以及如何将整个字符串放入弹出窗口?

欢迎所有答案。请帮助我。

谢谢你

【问题讨论】:

标签: asp.net gridview selectedindexchanged rowdatabound rowcommand


【解决方案1】:

如果你有一个实体绑定到你的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) + "...";
        }
    }
}

【讨论】:

  • 我将此代码置于 Protected void Grid_RowDataBound{} 下,但无法返回值...我该如何解决这个问题??
【解决方案2】:

我自己喜欢这个解决方案

100% 正常工作并经过全面测试

protected void grdName_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[8].Text.Length > 100)
                {
                    e.Row.Cells[8].Text = e.Row.Cells[8].Text.Substring(0, 12);
                }


            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2010-10-31
    • 2011-06-05
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多