0

I am extracting data from SQL Server and displaying it in a gridview. I am then trying to sort the columns out by clicking on the headers. I get a problem of unhandled event.... any help would be great thanks

The problem I get is that when I click a heading to sort out the table, I get the following error

General Error

The GridView '_propertyGridView' fired event Sorting which wasn't handled.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Mark-up

<asp:GridView ID="_propertyGridView" runat="server" CssClass="table table-hover table-striped" AutoGenerateColumns="false" AllowSorting="true" GridLines="None" OnRowCommand="PropertyRowCommand"  Width="100%">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Ref" SortExpression="Id" HeaderStyle-HorizontalAlign="Left"   />
        <asp:BoundField DataField="PostCode" HeaderText="Post Code" SortExpression="PostCode" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="ContractsFinishedOn" HeaderText="Contract Signed On" SortExpression="ContractsFinishedOn" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="Mobile" HeaderText="Mobile No." SortExpression="Mobile" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="LandLordEmail" HeaderText="Owners Email" SortExpression="LandLordEmail" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="MoveInDate" HeaderText="Move In Date" SortExpression="MoveInDate" HeaderStyle-HorizontalAlign="Left" />
        <asp:TemplateField HeaderText="Status" SortExpression="Status1">
            <ItemTemplate>
                <asp:CheckBox ID="_tenantPaymentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("TenantReceipt") %>' />
                <asp:LinkButton ID="_tenantPaymentLink" Text="Send Tenant Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="TenantEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the tenant, please make sure its correct');" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Status" SortExpression="Status2">
            <ItemTemplate>
                <asp:CheckBox ID="_landlordInfoCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordInfo") %>' />
                <asp:LinkButton ID="_landlordInfoLink" Text="Request Landlord Info" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordInfoEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the landlord, please make sure its correct');" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Status" SortExpression="Status3">
            <ItemTemplate >
                <asp:CheckBox ID="_landlordRentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordReceipt") %>' />
                <asp:LinkButton ID="_landlordRentLink" Text="Send Landlord Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the landlord, please make sure its correct');" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code-Behind

protected void _propertyGridView_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        string sortExpression = e.SortExpression;
        ViewState["z_sortexpresion"] = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, "DESC");
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, "ASC");
        }
    }
    catch (Exception ex)
    {
        SearchErrorLbl.Text = "error in such";
    }
}

public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set
    {
        ViewState["sortDirection"] = value;
    }
}

private void SortGridView(string sortExpression, string direction)
{
    DTSorting = new DataView(DTSorting, "", sortExpression + " " + direction, DataViewRowState.CurrentRows).ToTable();
    _propertyGridView.DataSource = DTSorting;
    _propertyGridView.DataBind();
}

public DataTable DTSorting
{
    get
    {
        if (ViewState["Sorting"] != null)
            return (DataTable)ViewState["Sorting"];
        else
            return null;
    }
    set
    {
        ViewState["Sorting"] = value;
    }
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Deviney
  • 123
  • 1
  • 1
  • 13

2 Answers2

2

Add OnSorting = "_propertyGridView_Sorting" to your gridview markup.

You define the handler in code, but you're not attaching it to the grid.

Yuriy Galanter
  • 35,167
  • 11
  • 60
  • 119
  • Thanks that worked but now when i click on the header it just reloads the page without sorting – Deviney Apr 10 '14 at 15:59
  • 1
    Debug `SortGridView` see whether sorting expression is being passed correctly, whether correct DataView is being generated. If there're still problems - ask a separate question, so it won't be confusing. – Yuriy Galanter Apr 10 '14 at 16:04
2

You need to attach _propertyGridView_Sorting event to GridView.

<asp:GridView OnSorting="_propertyGridView_Sorting" ...>
  ...
</asp:GridView>
Win
  • 56,078
  • 13
  • 92
  • 167
  • Thanks that worked but now when i click on the header it just reloads the page without sorting but thanks for that fix – Deviney Apr 10 '14 at 16:00