有人成功使用了 GridView.SortAscendingHeaderStyle 属性 吗?
Was anyone successful using the GridView.SortAscendingHeaderStyle property?
使用 .NET 4.0 框架,我尝试设置绑定到 SqlDataSource
的 GridView
的一些属性,以将样式应用于已排序的列。但是无论我使用什么BackColor, ForeColor, CssClass
,当查看渲染页面的源代码时,生成的<table>
没有任何classes或样式应用到headers(或细胞)。似乎网格视图控件忽略了对 SortAscendingHeaderStyle
或 SortDescendingHeaderStyle
属性的任何更改。
除了允许排序之外,是否需要为任何其他属性分配特定值才能使 SortAscendingHeaderStyle
或 SortDescendingHeaderStyle
起作用?或者是否有任何事件需要处理?
我只是想要一些改变,只是为了看到一列已经排序 - 无论发生什么改变(颜色、边框、字体 - 无关紧要),但都没有发生。
除了 AllowPaging
和 AllowSorting
设置为 True
,我将 AutoGenerateColumns
设置为 False
,其余 GridView
属性是使用默认值。
编辑:
阅读 Microsoft 关于 GridView.SortedAscendingHeaderStyle Property
的帮助后,我的大脑严重受伤。他们开始于:
Gets or sets the CSS style to apply to a GridView column heading when
the column is sorted in ascending order.
然后继续给出以下语法:
Public ReadOnly Property SortedAscendingHeaderStyle As TableItemStyle
这已经与第一行的粗体部分相矛盾了。情况越来越糟:
true if a style is applied to the GridView heading when the column is
sorted in ascending order; otherwise, false.
作者并不介意,属性 是 System.Web.UI.WebControls.TableItemStyle
class 的 object,而不仅仅是 boolean
true
或 false
。还是我遗漏了什么?
在多次尝试找出属性无效的原因后,我使用了 https://whosebug.com/a/10304546/2721750
中的解决方案
正如 https://whosebug.com/a/23894428/2721750 所指出的,应用样式的顺序很重要。
两种解决方案结合使用。流程应该是
- 将排序后的数据源分配给网格;
- 调用DataBind();
- 将 ascending/descending class 分配给 CssClass 到排序列的 header 行单元格。
private int GetColumnIndex(GridView grv, string SortExpression)
{
int i = 0;
foreach (DataControlField c in grv.Columns)
{
if (c.SortExpression == SortExpression)
break;
i++;
}
return i;
}
protected void grvSampleOrders_Sorting(object sender, GridViewSortEventArgs e)
{
Utility_Web.PersistSortDirection(Session, (((Control)sender)).ID, e.SortExpression, e.SortDirection);
grvSampleOrders.DataSource = Utility_Web.GetSortedDataView(Session, dsSampleOrders, (((Control)sender)).ID); // replace original SQL data source with its sorted copy
grvSampleOrders.DataBind();
grvSampleOrders.HeaderRow.Cells[GetColumnIndex(grvSampleOrders, e.SortExpression)].CssClass = Session["grvSampleOrders_sort"].ToString().Equals("ASC") ? "sortasc" : "sortdesc";
}
为了邀请答案,我暂时不会接受我的答案,因为它只提供了一种解决方法。
使用 .NET 4.0 框架,我尝试设置绑定到 SqlDataSource
的 GridView
的一些属性,以将样式应用于已排序的列。但是无论我使用什么BackColor, ForeColor, CssClass
,当查看渲染页面的源代码时,生成的<table>
没有任何classes或样式应用到headers(或细胞)。似乎网格视图控件忽略了对 SortAscendingHeaderStyle
或 SortDescendingHeaderStyle
属性的任何更改。
除了允许排序之外,是否需要为任何其他属性分配特定值才能使 SortAscendingHeaderStyle
或 SortDescendingHeaderStyle
起作用?或者是否有任何事件需要处理?
我只是想要一些改变,只是为了看到一列已经排序 - 无论发生什么改变(颜色、边框、字体 - 无关紧要),但都没有发生。
除了 AllowPaging
和 AllowSorting
设置为 True
,我将 AutoGenerateColumns
设置为 False
,其余 GridView
属性是使用默认值。
编辑:
阅读 Microsoft 关于 GridView.SortedAscendingHeaderStyle Property
的帮助后,我的大脑严重受伤。他们开始于:
Gets or sets the CSS style to apply to a GridView column heading when the column is sorted in ascending order.
然后继续给出以下语法:
Public ReadOnly Property SortedAscendingHeaderStyle As TableItemStyle
这已经与第一行的粗体部分相矛盾了。情况越来越糟:
true if a style is applied to the GridView heading when the column is sorted in ascending order; otherwise, false.
作者并不介意,属性 是 System.Web.UI.WebControls.TableItemStyle
class 的 object,而不仅仅是 boolean
true
或 false
。还是我遗漏了什么?
在多次尝试找出属性无效的原因后,我使用了 https://whosebug.com/a/10304546/2721750
中的解决方案正如 https://whosebug.com/a/23894428/2721750 所指出的,应用样式的顺序很重要。
两种解决方案结合使用。流程应该是
- 将排序后的数据源分配给网格;
- 调用DataBind();
- 将 ascending/descending class 分配给 CssClass 到排序列的 header 行单元格。
private int GetColumnIndex(GridView grv, string SortExpression)
{
int i = 0;
foreach (DataControlField c in grv.Columns)
{
if (c.SortExpression == SortExpression)
break;
i++;
}
return i;
}
protected void grvSampleOrders_Sorting(object sender, GridViewSortEventArgs e)
{
Utility_Web.PersistSortDirection(Session, (((Control)sender)).ID, e.SortExpression, e.SortDirection);
grvSampleOrders.DataSource = Utility_Web.GetSortedDataView(Session, dsSampleOrders, (((Control)sender)).ID); // replace original SQL data source with its sorted copy
grvSampleOrders.DataBind();
grvSampleOrders.HeaderRow.Cells[GetColumnIndex(grvSampleOrders, e.SortExpression)].CssClass = Session["grvSampleOrders_sort"].ToString().Equals("ASC") ? "sortasc" : "sortdesc";
}
为了邀请答案,我暂时不会接受我的答案,因为它只提供了一种解决方法。