【问题标题】:Gridview sorting when Header text changed in RowDataBound event在 RowDataBound 事件中更改标题文本时的 Gridview 排序
【发布时间】:2011-09-27 07:50:51
【问题描述】:

我的网页上有一个 GridView,它的 DataSource 是一个在运行时填充的 DataTable。 GridView 的 AllowSorting 属性为 True。我已经成功地为这个 GridView 实现了手动排序。

但我必须将网页翻译成其他语言,我使用本地资源文件。我在 RowDataBound 事件中更改了 GridView 列的标题文本。从那时起,我无法对 GridView 进行排序。

       protected void GVSummaryTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.Cells.Count > 0)
    {
        //Translate header text
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = GetLocalResourceObject("GVSummaryTableName").ToString();
            e.Row.Cells[1].Text = GetLocalResourceObject("GVSummaryTableLoginLevel").ToString();
            e.Row.Cells[2].Text = GetLocalResourceObject("GVSummaryTableLoginID").ToString();
            e.Row.Cells[4].Text = GetLocalResourceObject("GVSummaryTableDate").ToString();

        }

    }

}

我应该怎么做才能对列进行排序? 任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    将代码更改为以下解决了问题:

              if (e.Row.RowType == DataControlRowType.Header) 
            { 
                LinkButton LnkHeaderText = e.Row.Cells[1].Controls[0] as LinkButton; 
                LnkHeaderText.Text = "Name"; 
            }
    

    【讨论】:

    • 我们需要将其更改为链接按钮的原因是什么
    【解决方案2】:

    我不确定问题是否与标题文本有关,因为排序通常是使用 排序表达式 完成的。请确保您在进行排序时也为此属性赋值。希望这会有所帮助!

    【讨论】:

    • 在运行时,当添加排序表达式属性时,标题将自身更改为链接按钮。但是当我们动态更改标题文本时,它会变为文本,因此我们也必须将其动态更改为链接按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多