Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将css类应用于以DataTable作为源进行排序时的GridView标头?_C#_Asp.net_Sorting_Gridview_Webforms - Fatal编程技术网

C# 将css类应用于以DataTable作为源进行排序时的GridView标头?

C# 将css类应用于以DataTable作为源进行排序时的GridView标头?,c#,asp.net,sorting,gridview,webforms,C#,Asp.net,Sorting,Gridview,Webforms,我的GridView控件有问题。在后期排序数据绑定之后,我无法将css类保留在已排序的标题上 我的网格视图: <asp:GridView CssClass="grid-view shadow" ID="GridView1" runat="server" Caption="Application Owners" AutoGenerateColumns="false" PageSize="10" ShowHeaderWhenEmpty="true" EmptyDataText="No info

我的GridView控件有问题。在后期排序数据绑定之后,我无法将css类保留在已排序的标题上

我的网格视图:

<asp:GridView CssClass="grid-view shadow" ID="GridView1" runat="server" Caption="Application Owners" AutoGenerateColumns="false" PageSize="10" ShowHeaderWhenEmpty="true" EmptyDataText="No information found." AllowSorting="true" AllowPaging="true" OnSorting="GridView1_Sorting" >
                <PagerSettings FirstPageText="First" PreviousPageText="Previous" NextPageText="Next" LastPageText="Last" Visible="False" />
                <HeaderStyle CssClass="header" />
                <FooterStyle ForeColor="MediumBlue" BackColor="LightCyan" />
                <RowStyle CssClass="odd-row" />
                <AlternatingRowStyle CssClass="even-row" />
                <SortedAscendingHeaderStyle CssClass="sort-asc" />
                <SortedDescendingHeaderStyle CssClass="sort-desc" />
                <Columns>
                    <asp:BoundField DataField="ClientName" HeaderText="Client" SortExpression="ClientName" />
                    <asp:BoundField DataField="CompanyName" HeaderText="Company" SortExpression="CompanyName" />
                    <asp:BoundField DataField="HostName" HeaderText="HostName" SortExpression="HostName" />
                    <asp:BoundField DataField="InstanceName" HeaderText="Instance" SortExpression="InstanceName" />
                    <asp:BoundField DataField="SystemType" HeaderText="SystemType" SortExpression="SystemType" ItemStyle-Wrap="True" />
                    <asp:BoundField DataField="DatabaseName" HeaderText="Database" SortExpression="DatabaseName" />
                    <asp:BoundField DataField="BackupStrategyName" HeaderText="Strategy" SortExpression="BackupStrategyName" />
                    <asp:BoundField DataField="BackupType" HeaderText="Type" SortExpression="BackupType" />
                    <asp:BoundField DataField="BackupLocation" HeaderText="Location" SortExpression="BackupLocation" />
                    <asp:BoundField DataField="EngineName" HeaderText="Engine" SortExpression="EngineName" />
                    <asp:BoundField DataField="Comments" HeaderText="SequenceType" SortExpression="Comments" />
                    <asp:BoundField DataField="LastModified" HeaderText="LastModified" SortExpression="LastModified" />
                </Columns>
            </asp:GridView>

在这之前,它就像一个符咒。我的问题是,当我转到下一页或执行任何需要到GridView的新数据绑定的操作时,标题的css类会被删除。我对C比较陌生,所以我不知道如何使用视图状态,如果这是一个可能的解决方案的话。谢谢大家!

当您进行初始数据绑定时,将其放入if!Page.IsPostBack{GridView1.DataBind;}我在if!IsPostBack块。
 public void GridViewSort(object sender, GridViewSortEventArgs e)
    {
        SortDirection _sortDirection;
        SortDirection _lastSortDir = SortDirection.Ascending;
        string _sortDir;
        string _cssClass;
        int _columnIndex = 0;

        // Get the last sort direction from the DataTable`s DefaultView property.
        if (tempTable.DefaultView.Sort.ToString().Contains("ASC"))
        {
            _lastSortDir = SortDirection.Ascending;
        }
        else if (tempTable.DefaultView.Sort.ToString().Contains("DESC"))
        {
            _lastSortDir = SortDirection.Descending;
        }

        // Check if it`s the first time sorting the GridView.
        if (String.IsNullOrEmpty(tempTable.DefaultView.Sort.ToString()))
        {
            _sortDirection = SortDirection.Ascending;
        }
        else
        {
            // If the sort is on the same column as the previous sort then change the sorting order.
            if (tempTable.DefaultView.Sort.ToString().Contains(e.SortExpression.ToString()))
            {
                _sortDirection = (_lastSortDir == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending;
            }
            // If it`s a different column then sort it in Ascending order.
            else
            {
                _sortDirection = SortDirection.Ascending;
            }
        }

        // Set the DefaultView.Sort propery of the DataTable and rebind it to the GridView.
        // Add the sort oreder icon to the filtered header.
        _sortDir = (_sortDirection == SortDirection.Ascending) ? "ASC" : "DESC";
        _cssClass = (_sortDirection == SortDirection.Ascending) ? "sort-asc" : "sort-desc";
        tempTable.DefaultView.Sort = e.SortExpression.ToString() + " " + _sortDir.ToString();

        GridView1.DataSource = tempTable;
        GridView1.DataBind();

        // Find GridView header and add sorting image class
        foreach (DataControlFieldHeaderCell headerCell in GridView1.HeaderRow.Cells)
        {
            if (headerCell.ContainingField.SortExpression == e.SortExpression)
            {
                _columnIndex = GridView1.HeaderRow.Cells.GetCellIndex(headerCell);
            }
        }
        GridView1.HeaderRow.Cells[_columnIndex].CssClass = _cssClass;
    }