按列排序网格

时间:2018-11-16 08:31:34

标签: c#

我想在同时使用UpdataPanel的MultiView中对Gridview进行排序。当我运行程序并单击GridView列时,它没有排序,但是当我检查页面时显示以下错误:

  

MsAjaxJs?v = c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1未捕获   错误:Sys.WebForms.PageRequestManagerServerErrorException:对象   引用未设置为对象的实例。       在Function.Error.create(MsAjaxJs?v = c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1)中       在Sys.WebForms.PageRequestManager._createPageRequestManagerServerError   (MsAjaxJs?v = c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1)

我的源代码:

GridView代码:

<asp:GridView ID="grdProcessed" runat="server" AutoGenerateColumns="False" CellPadding="4"
                       ForeColor="#333333" GridLines="Both" EnableViewState="false"
                        ShowFooter="True" width="100%" AllowPaging="True" AutoResize="false"  AllowSorting="true" OnSorting="GridProcessed_Sorting"  OnRowDataBound="ProcessedGrid_RowDataBound" OnPageIndexChanging="GridViewProcessed_PageIndexChanging" PageSize="20" PagerStyle-Font-Size="Large"  > 
                        <PagerStyle CssClass="gvPagerCss"/>
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

                        <Columns>
                            <asp:BoundField DataField="PanicID" HeaderText="PanicID"  InsertVisible="False" Visible="False" ReadOnly="True" >
                                <HeaderStyle HorizontalAlign="Left" />

                            </asp:BoundField>
                            <asp:BoundField DataField="Reference" HeaderText="Reference"   ReadOnly="true" SortExpression="Reference" >
                                <HeaderStyle HorizontalAlign="Left" />

                            </asp:BoundField>
                            <asp:BoundField DataField="IDNumber" HeaderText="ID number"  ReadOnly="true"  SortExpression="IDNumber" >
                                <HeaderStyle HorizontalAlign="Left"  />
                            </asp:BoundField>
                            <asp:BoundField DataField="FirstName" HeaderText="First name"  SortExpression="FirstName">
                                <HeaderStyle HorizontalAlign="Left" />

                            </asp:BoundField>
                            <asp:BoundField DataField="Surname" HeaderText="Last name"  SortExpression="Surname">
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:BoundField>
                            <asp:BoundField DataField="EmailAddress" HeaderText="Email address"   SortExpression="EmailAddress">
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:BoundField>
                            <asp:BoundField DataField="CellNumber" HeaderText="Cell number"   SortExpression="CellNumber">
                                <HeaderStyle HorizontalAlign="Left"  />
                            </asp:BoundField>
                            <asp:BoundField DataField="PanicRequestDate" HeaderText="Date panic requested"  SortExpression="PanicRequestDate" DataFormatString="{0:dd-MM-yyyy/hh:mm}">
                                <HeaderStyle HorizontalAlign="Left"  />
                            </asp:BoundField>
                            <asp:BoundField DataField="ProcessedBy" HeaderText="Processed by"  SortExpression="ProcessedBy">
                                <HeaderStyle HorizontalAlign="Left"  />
                            </asp:BoundField>
                            <asp:BoundField DataField="DateProcessed" HeaderText="Date panic processed"  SortExpression="DateProcessed" DataFormatString="{0:dd-MM-yyyy/hh:mm}">
                                <HeaderStyle HorizontalAlign="Left"  />
                            </asp:BoundField>

CS文件:

public WebGrid Grid { get { return gRd; } }

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["user"] == null)
    Response.Redirect("~/Account/Login");

    SetLinkLabels();
}


protected void GridProcessed_Sorting(object sender, GridViewSortEventArgs e)
{


    DataSet dsSortTable = grdProcessed.DataSource as DataSet;
    DataTable dtSortTable = dsSortTable.Tables[0];
    DataView dvSortedView = new DataView(dtSortTable);

    if ((ViewState["sorting"]).ToString() =="DESC" | ViewState["sorting"] == null)
    {
        dvSortedView.Sort = e.SortExpression + " ASC";
        ViewState["sorting"] = "ASC";
    }
    else if(ViewState["sorting"].ToString()=="ASC")
    {
        dvSortedView.Sort = e.SortExpression + " DESC";
        ViewState["sorting"] = " DSC";
    }

    grdProcessed.DataSource = dvSortedView;
    grdProcessed.DataBind();

}

0 个答案:

没有答案