Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DataView dataView = (DataView)Session["kingdomData"];
- dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
- gvAllData.DataSource = dataView;
- gvAllData.DataBind();
- private string ConvertSortDirectionToSql(SortDirection sortDirection)
- {
- string newSortDirection = String.Empty;
- if (Session["SortDirection"] == null)
- {
- switch (sortDirection)
- {
- case SortDirection.Ascending:
- newSortDirection = "ASC";
- break;
- case SortDirection.Descending:
- newSortDirection = "DESC";
- break;
- }
- }
- else
- {
- newSortDirection = Session["SortDirection"].ToString();
- switch (newSortDirection)
- {
- case "ASC":
- newSortDirection = "DESC";
- break;
- case "DESC":
- newSortDirection = "ASC";
- break;
- }
- }
- Session["SortDirection"] = newSortDirection;
- return newSortDirection;
- }
- DataView dv = new DataView(dt);
- ArrayList lst = new ArrayList();
- lst.AddRange(dv.Table.Rows);
- lst.Sort(new MyComparer());
- foreach (DataRow dr in lst)
- Debug.WriteLine(dr[0]);
- class MyComparer : IComparer
- {
- public int Compare(object x, object y)
- {
- DataRow rx = x as DataRow;
- DataRow ry = y as DataRow;
- string datax = (string)rx[colName];
- string datay = (string)ry[colName];
- // Process datax and datay here then compare them (ASC)
- return datax.CompareTo(datay);
- }
- }
- int cmp = colAx.CompareTo(colAy);
- if (cmp != 0)
- return cmp;
- cmp = colBy.CompareTo(colBx);
- return cmp;
- dataview.sort = e.sortexpression + " " + e.Sortdirection;
- DataView dv = GetDataViewSomewhere();
- //Naturally sort by COLUMN_TO_SORT_ON
- try
- {
- List<string> rowList = new List<string>();
- foreach (DataRowView drv in dv)
- rowList.Add((string)drv["COLUMN_TO_SORT_ON"]);
- rowList.Sort(new NaturalComparer());
- Dictionary<string, int> sortValueHash = new Dictionary<string, int>();
- for (int i = 0; i < rowList.Count; i++)
- sortValueHash.Add(rowList[i], i);
- dv.Table.Columns.Add("NATURAL_SORT_ORDER", typeof(int));
- foreach (DataRowView drv in dv)
- drv["NATURAL_SORT_ORDER"] = sortValueHash[(string)drv["COLUMN_TO_SORT_ON"]];
- dv.Sort = "NATURAL_SORT_ORDER";
- }
- catch (Exception)
- {
- DEBUG_TRACE("Could not naturally sort");
- dv.Sort = "COLUMN_TO_SORT_ON";
- }
Add Comment
Please, Sign In to add comment