0

如何启用排序以在 GridView 中动态设置 lbl 字段文本。下面是 GridView 和 .cs 代码的代码,我在其中设置动态标签字段的值。问题是,我无法对这个特定的 asp:template 应用类似于其他模板的排序表达式。

<asp:GridView ID="gvAlertsStatus" runat="server" AllowSorting="true"
    OnSorting="gvAlertsStatus_Sorting" AutoGenerateColumns="false"
    OnRowDataBound="gvAlertsStatus_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Owner" SortExpression="UsrName"
            ItemStyle-Width="120px">
            <ItemTemplate><%#Eval("UsrName") %></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last Result" SortExpression="lblLastResult"
            ItemStyle-Width="40px" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblLastResult" Font-Bold="true">
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Active" SortExpression="Active"
            ItemStyle-Width="60px" >
            <ItemTemplate>
                <asp:Label runat="server" ID="lblActive"
                    Text='<%#Eval("Active") %>' >
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.aspx.cs 代码

public void gvAlertsStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex >= 0)
    {
         Label lblLastResult = (Label)e.Row.FindControl("lblLastResult");
        // Get the dates
        DateTime dtSuccess = (eScan.Lib.Shared.Utils.IsDate(lblSuccess.Text)) ?
            DateTime.Parse(lblSuccess.Text) : DateTime.MinValue;
        DateTime dtFailure = (eScan.Lib.Shared.Utils.IsDate(lblFailure.Text)) ?
            DateTime.Parse(lblFailure.Text) : DateTime.MinValue;
        DateTime dtDelay = (eScan.Lib.Shared.Utils.IsDate(lblDelay.Text)) ?
            DateTime.Parse(lblDelay.Text) : DateTime.MinValue;
        DateTime dtLastRun = (dtSuccess > dtFailure) ? dtSuccess : dtFailure;

        // Set up the Last Result label
        lblLastResult.Text = (dtSuccess > dtFailure) ? "Success" : "Fail";            
    }
}

protected void gvAlertsStatus_Sorting(object sender, GridViewSortEventArgs e)
{
    // Set up the sort direction
    SortDirection sd = SortDirection.Ascending;

    // If the same column is clicked, then alternate sort direction
    if (e.SortExpression.Equals(ViewState["SortExp"]))
    {
        sd = ((SortDirection)ViewState["SortDir"] == SortDirection.Descending) ?
            SortDirection.Ascending : SortDirection.Descending;
    }

    // Save the states
    ViewState["SortExp"] = e.SortExpression;
    ViewState["SortDir"] = sd;

    // Sort the view and rebind that 
    DataView dv = (DataView)gvAlertsStatus.DataSource;
    dv.Sort = e.SortExpression + " " +
        ((sd == SortDirection.Descending) ? "DESC" : "ASC");

    gvAlertsStatus.DataSource = dv;
    gvAlertsStatus.DataBind();
}
4

1 回答 1

0

试试这个代码…………

protected void RadgvData_SortCommand(object sender, GridSortCommandEventArgs e)
        {
            GridTableView tableView = e.Item.OwnerTableView;
            e.Canceled = true;
            GridSortExpression expression = new GridSortExpression();
            expression.FieldName = e.SortExpression;
            if (tableView.SortExpressions.Count == 0 || tableView.SortExpressions[0].FieldName != e.SortExpression)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
            {
                expression.SortOrder = GridSortOrder.Ascending;
            }
            else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
            {
                expression.SortOrder = GridSortOrder.Descending;
            }

            tableView.SortExpressions.AddSortExpression(expression);
            RadgvData.Rebind();

        }
于 2013-04-16T09:35:20.433 回答