我的网页中有一个gridview来显示我的查询结果
我编写了必要的函数,通过单击列标题进行排序
但是当我更改后面代码中的标题时,我无法通过单击
进行排序如何在绑定(bind)数据后将列格式从文本更改为超链接?
最佳答案
我也已经遇到了这个“错误”。技巧是避免在更改 Header 的 Text 值时将其转换为字符串(您可能在后面的代码中使用 e.Row.Cells[0].Text = "This ischanged header 1")。此后,所有更改的列都将禁用排序。因此,您需要将 header 视为 LinkButton,而不是文本,一切都会完美。
这是我的解决方案:
我在网格定义中使用 OnRowDataBound="Exmplgrid_RowDataBound"。如果您要使用此功能,排序将在前 3 列中保持启用状态,并在第 4 列中禁用(用于演示目的):
protected void Exmplgrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton btnSort = (LinkButton)e.Row.Cells[0].Controls[0];
btnSort.Text = "This is changed header 1";
btnSort = (LinkButton)e.Row.Cells[1].Controls[0];
btnSort.Text = "This is changed header 2";
btnSort = (LinkButton)e.Row.Cells[2].Controls[0];
btnSort.Text = "This is changed header 2";
e.Row.Cells[3].Text = "This is changed header 2. SORTING DISABLED"
}
}
关于c# - 如何在更改标题文本后在 gridview 中启用排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15469877/