问题描述
是用datasource绑定的数据源,gridview的allowsorting设置为true,列的sortexpression也设置了,后台排序代码也有,但是左键点击标题却不能拍戏,右键>打开却可以触发排序。前台<asp:GridViewID="GridView1"runat="server"AllowSorting="True"EnablePersistedSelection="True"EnableSortingAndPagingCallbacks="True"HorizontalAlign="Center"OnSorting="GridView1_Sorting"><Columns><asp:BoundFieldDataField="序号"HeaderText="新闻序号"SortExpression="序号"><ItemStyleWrap="True"/></asp:BoundField><asp:BoundFieldDataField="标题"HeaderText="新闻标题"><HeaderStyleWidth="300px"/></asp:BoundField><asp:BoundFieldDataField="发布日期"HeaderText="发布日期"><HeaderStyleWidth="100px"/></asp:BoundField></Columns></asp:GridView>
后台protectedvoidPage_Load(objectsender,EventArgse){if(!Page.IsPostBack){bindgrid();}}protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse){if(ViewState["SortDirection"]==null){ViewState["SortDirection"]="DESC";}if(ViewState["SortDirection"].ToString()=="ASC"){ViewState["SortDirection"]="DESC";}else{ViewState["SortDirection"]="ASC";}ViewState["SortExpression"]=e.SortExpression;bindgrid();}voidbindgrid(){stringstr="select序号,标题,convert(varchar(10),变更日期,20)as发布日期fromnewsorderby序号desc";SqlConnectionmyc=newSqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);SqlDataAdaptermyda=newSqlDataAdapter(str,myc);DataSetds=newDataSet();myda.Fill(ds);if(ViewState["SortDirection"]==null){GridView1.DataSource=ds.Tables[0].DefaultView;}else{DataViewSortedDV=newDataView(ds.Tables[0]);SortedDV.Sort=ViewState["SortExpression"].ToString()+""+ViewState["SortDirection"].ToString();GridView1.DataSource=SortedDV;}this.GridView1.DataKeyNames=newstring[]{"标题"};GridView1.DataBind();}
解决方案
本帖最后由 qq_15360881 于 2015-01-01 13:04:27 编辑