ASP.NET gridview does not allow me to click on column header texts so I can sort the data

我是研究僧i 提交于 2019-12-13 22:08:03

问题


I'm just trying to allow the user to sort the GridView by any of the columns of it.

<asp:GridView ID="gvShows" runat="server" DataKeyNames="dataSource,title" Caption="Show List" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" CaptionAlign="Left" OnSorting="gvShows_Sorting" >
                    <RowStyle BorderColor="Black" />
                    <Columns> 
                        <asp:TemplateField HeaderText="Select"> 
                            <ItemTemplate> 
                                <asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false"/> 
                            </ItemTemplate> 
                        </asp:TemplateField> 
                        <asp:BoundField HeaderText="Data Source" DataField="dataSource" /> 
                        <asp:BoundField HeaderText="Show ID" DataField="ShowId" /> 
                        <asp:BoundField HeaderText="Show Title" DataField="title" /> 
                        <asp:BoundField HeaderText="Episode Id" DataField="EpisodeID" /> 
                        <asp:BoundField HeaderText="Episode Title" DataField="EpisodeTitle" /> 
                        <asp:BoundField HeaderText="Genre" DataField="Genre" /> 
                        <asp:BoundField HeaderText="Show Type Description" DataField="ShowTypeDescription" /> 
                        <asp:BoundField HeaderText="Director Name" DataField="DirectorName" /> 
                        <asp:BoundField HeaderText="Release Year" DataField="ReleaseYear" /> 
                        <asp:BoundField HeaderText="Season Episode" DataField="SeasonEpisode" /> 
                    </Columns>  
                </asp:GridView>

    protected void gvShows_Sorting(object sender, GridViewSortEventArgs e)
    {
        var dataTable = Session["shows"] as DataTable;

        if (dataTable != null)
        {
            var dataView = new DataView(dataTable)
                {
                    Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection)
                };

            gvShows.DataSource = dataView;
            gvShows.DataBind();
        }
    }

    private string ConvertSortDirection(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

When data is displayed in the GridView, I'm simply not allowed to click on the header text so that I can sort the data:


回答1:


You need to add "SortExpression" in your asp:BoundField.

For example :

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />



回答2:


I got it. I needed to add SortExpressions because the columns are not generated automatically.



来源:https://stackoverflow.com/questions/17158405/asp-net-gridview-does-not-allow-me-to-click-on-column-header-texts-so-i-can-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!