排序以在GridView中动态添加lbl字段

时间:2013-03-22 15:58:22

标签: c# asp.net sorting gridview

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

<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();
}

1 个答案:

答案 0 :(得分: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();

        }