本文介绍了列标题单击时排序gridview列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好朋友, 正在使用asp.net c#和SqlDatabase。 我想要排序gridview列。总计我在gridview中有8列,只有当列标题点击时我才想排序6列。 当列标题点击时,它必须按升序对列进行排序。 请你帮我,怎么做。 谢谢Hello frnds,am working on asp.net c# and SqlDatabase.I want to Sort a gridview columns. Total I have 8 columns in my gridview, only i want to sort 6 columns when column header click.It must sort the column in ascending order when column header clicks.Please can you help me, how to do this.Thanks推荐答案<asp:boundfield datafield="ProductCode" headertext="Product Code" xmlns:asp="#unknown">SortExpression="ProductCode" /></asp:boundfield> i取了我的gridview id = Productlist 那么在C#i have taken my gridview id =ProductlistTHEN IN C#//sorting Populate and load GridView on Page_Load Event private DataTable BindGridView() { DataTable dtGrid = new DataTable(); string strSelect = "YOUR SELECT QUERY"; OleDbCommand cmd = new OleDbCommand(strSelect, con); OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd); dAdapter.Fill(dtGrid); return dtGrid; } //Create Public Property of SortDirection type and store direction in ViewState. public SortDirection dir { get { if (ViewState["dirState"] == null) { ViewState["dirState"] = SortDirection.Ascending; } return (SortDirection)ViewState["dirState"]; } set { ViewState["dirState"] = value; } } //Check Gridview's current direction from ViewState and set new sort direction in Sorting Event. protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e) { string sortingDirection = string.Empty; if (dir == SortDirection.Ascending) { dir = SortDirection.Descending; sortingDirection = "Desc"; } else { dir = SortDirection.Ascending; sortingDirection = "Asc"; } DataView sortedView = new DataView(BindGridView()); sortedView.Sort = e.SortExpression + " " + sortingDirection; Productlist.DataSource = sortedView; Productlist.DataBind(); } 快乐编码!! :)Happy Coding!!:) 这篇关于列标题单击时排序gridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-12 13:24