c# - Multiple column sorting in Gridview? -
hi want sort multiple column in gridview shown here hierarchical (multi-column) sorting .net gridview?
i did home work aspx looks
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="_default" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="head1" runat="server"> <title>multiple sorting gridview</title> </head> <body> <form id="form1" runat="server"> <div> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="true" allowsorting="true" onsorting="gridview1_sorting" style="margin-right: 541px" width="873px"> </asp:gridview> <p><asp:label runat="server" id="lblsortexpression" /></p> </div> </form> </body> </html>
in aspx.cs page
using system; using system.collections; using system.collections.generic; using system.data; using system.diagnostics; using system.collections.specialized; using system.text; using system.web.ui.webcontrols; using system.data.sqlclient; partial class _default : system.web.ui.page { private listdictionary m_ldsortexpression; private listdictionary sortexpressions { { m_ldsortexpression = (listdictionary)viewstate["sortexpressions"]; if (m_ldsortexpression == null) { m_ldsortexpression = new listdictionary(); } return m_ldsortexpression; } set { viewstate["sortexpressions"] = value; } } protected void page_load(object sender, system.eventargs e) { if (!ispostback) { binddata(); } } public void binddata() { // data database. // simplify code, fill datatable standard data. datatable dt = new datatable(); dt.columns.add("firstname"); dt.columns.add("lastname"); dt.columns.add("age"); dt.columns.add("position"); dt.rows.add(new object[] { "1", "2", 28, "5" }); dt.rows.add(new object[] { "2", "8", 31, "2" }); dt.rows.add(new object[] { "2", "4", 31, "4" }); dt.rows.add(new object[] { "3", "7", 37, "3" }); dt.rows.add(new object[] { "4", "4", 40, "1" }); dataview dv = dt.defaultview; // use stringbuilder hold sortexpression dataview stringbuilder sbsortexpression = new stringbuilder(); if (sortexpressions.count > 0) { string[] mykeys = new string[sortexpressions.count + 1]; sortexpressions.keys.copyto(mykeys, 0); (int = 0; <= sortexpressions.count - 1; i++) { sbsortexpression.append(mykeys[i]); sbsortexpression.append(" "); sbsortexpression.append(sortexpressions[mykeys[i]]); if (i != sortexpressions.count - 1) { sbsortexpression.append(", "); } } lblsortexpression.text = sbsortexpression.tostring(); // send sort-expression sql via stored-procedure dv.sort = sbsortexpression.tostring(); } else { lblsortexpression.text = string.empty; } gridview1.datasource = dv; gridview1.databind(); positionglyph(gridview1, dv.sort.tostring(), dt); } private void positionglyph(gridview gridview1, string p,datatable dt) { if ((gridview1.rows.count == 0) || (string.isnullorempty(p))) return; image glyph = new image(); glyph.enabletheming = false; string[] words = p.split(','); foreach (string word in words) { string[] sorttype = word.split(' '); if (sorttype[sorttype.length - 1] == sortorder.ascending.tostring().substring(0, 3).toupper()) glyph.imageurl = "~/images/down_arrow.png"; else glyph.imageurl = "~/images/up_arrow.png"; int columnindex = dt.columns[sorttype[sorttype.length - 2].tostring()].ordinal; gridview1.headerrow.cells[columnindex].controls.add(glyph); //for (int x = 0; x < dt.columns.count; x++) //{ // if (sorttype[sorttype.length - 2].tostring()== dt.columns[x].columnname) // { // gridview1.headerrow.cells[x].controls.add(glyph); // break; // } //} } } protected void gridview1_sorting(object sender, system.web.ui.webcontrols.gridviewsorteventargs e) { m_ldsortexpression = sortexpressions; if (!m_ldsortexpression.contains(e.sortexpression)) { m_ldsortexpression.add(e.sortexpression, e.sortdirection.tostring().replace("ascending", "asc").replace("descending", "desc")); } else { // sort direction string strsortdirection = m_ldsortexpression[e.sortexpression].tostring(); // ascending? if (strsortdirection == "asc") { // yes, sort in desc m_ldsortexpression[e.sortexpression] = "desc"; } else if (strsortdirection == "desc") { // descending // remove sort order m_ldsortexpression.remove(e.sortexpression); } } sortexpressions = m_ldsortexpression; binddata(); } public _default() { load += page_load; } }
everything working fine multiple sorting sort arrow in column header not showing properly.i think issue in positionglyph method.asc or desc arrow showing last clicked header.i want show sort direction columns.please me on same
you can show arrow sorting behavior of gridview column in rowcreated event way
protected void gridview1_rowcreated(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.header) { foreach (tablecell tc in e.row.cells) { if (tc.hascontrols()) { // search header link linkbutton lnk = (linkbutton)tc.controls[0]; if (lnk != null && gridview1.sortexpression == lnk.commandargument) { // inizialize new image system.web.ui.webcontrols.image img = new system.web.ui.webcontrols.image(); // setting dynamically url of image img.imageurl = "~/img/ico_" + (gridview1.sortdirection == sortdirection.ascending ? "asc" : "desc") + ".gif"; // adding space , image header link tc.controls.add(new literalcontrol(" ")); tc.controls.add(img); } } } } }
it toggles image on ascending , descending sort orders of column
what code loops through gridview header search linkbutton (the framework creates if sortexpression property set). then, if found linkbutton sorted field, shows image output, that's all
please see this sample
Comments
Post a Comment