C# ASP.NET gridview不允许我单击列标题文本,以便对数据进行排序
我只是尝试允许用户按GridView的任何列对其进行排序C# ASP.NET gridview不允许我单击列标题文本,以便对数据进行排序,c#,asp.net,sorting,gridview,C#,Asp.net,Sorting,Gridview,我只是尝试允许用户按GridView的任何列对其进行排序 <asp:GridView ID="gvShows" runat="server" DataKeyNames="dataSource,title" Caption="Show List" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" CaptionAlign="Left" OnSorting="gvShows_Sorting" >
<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;
}
受保护的void gvu排序(对象发送器、GridViewSortEventArgs e)
{
var dataTable=会话[“显示”]为dataTable;
if(dataTable!=null)
{
var dataView=新数据视图(数据表)
{
排序=e.SortExpression+“”+转换器方向(e.SortDirection)
};
gvShows.DataSource=dataView;
gvShows.DataBind();
}
}
专用字符串转换器SortDirection(SortDirection SortDirection)
{
string newSortDirection=string.Empty;
开关(排序方向)
{
案例排序方向。升序:
newSortDirection=“ASC”;
打破
案例排序方向。下行:
newSortDirection=“DESC”;
打破
}
返回新闻方向;
}
当数据显示在GridView中时,我不允许单击标题文本以便对数据进行排序:
您需要在asp:BoundField中添加“SortExpression”
例如:
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
您需要在asp:BoundField中添加“SortExpression”
例如:
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
我明白了。我需要添加SortExpressions,因为列不是自动生成的。我知道了。我需要添加SortExpressions,因为这些列不是自动生成的。看看这是否有帮助。我得到了它。我需要添加SortExpressions,因为这些列不是自动生成的。这里有一篇相关文章,请参阅,看看这是否有帮助。我得到了它。我需要添加SortExpressions,因为这些列不是自动生成的。这里有一篇相关文章,你应该选择Fabzien的答案-在你发布答案之前,他给了你正确的答案。。。你应该选择Fabzien的答案-在你发布答案之前,他给了你正确的答案。。。信用到期时的信用