999ANDRE999 Posted December 20, 2016 at 09:52 AM Report Share #601416 Posted December 20, 2016 at 09:52 AM Boas pessoal, Necessito de ordenar a minha gridview a partir da coluna. Tentei utilizar o sorting mas sem sucesso. adicionei na gridview: <asp:GridView ... OnSorting="gridView1_Sorting" AllowSorting="true"> E no CodeBehind: private string ConverterSortDirection(SortDirection sortDirection) { string novaDirecao = String.Empty; switch (sortDirection) { case SortDirection.Ascending: novaDirecao = "ASC"; break; case SortDirection.Descending: novaDirecao = "DESC"; break; } return novaDirecao; } protected void gridView1_Sorting(object sender, GridViewSortEventArgs e) { DataTable dataTable = gridView1.DataSource as DataTable; if (dataTable != null) { DataView dataView = new DataView(dataTable); dataView.Sort = e.SortExpression + " " + ConverterSortDirection(e.SortDirection); gridView1.DataSource = dataView; gridView1.DataBind(); } } Mas não faz nada. Alguem consegue ajudar? Link to comment Share on other sites More sharing options...
999ANDRE999 Posted December 20, 2016 at 02:50 PM Author Report Share #601424 Posted December 20, 2016 at 02:50 PM Coloquei este código e já não dá erros nenhuns, mas ao clicar no nome da coluna não faz nada, só pisca, tipo um refresh public string SortingExpression { get { if (this.ViewState["SortExpression"] == null) return ""; else return (string)this.ViewState["SortExpression"]; } set { this.ViewState["SortExpression"] = value; } } protected void Sorting(object sender, GridViewSortEventArgs e) { DataTable m_DataTable = GridView_phone.DataSource as DataTable; if (m_DataTable != null) { DataView m_DataView = new DataView(m_DataTable); SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC"); m_DataView.Sort = SortingExpression; GridView_phone.DataSource = m_DataView; GridView_phone.DataBind(); } } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now