Answered by:
Display data in descending order

Question
-
User-2055741253 posted
Hello All,
How can I show the data in GridView in descending order as default?
Here is my Code:
aspx code: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DisplayTextFile.aspx.cs" Inherits="CSV_to_Browser.DisplayTextFile" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <link href="StyleSheet1.css" rel="stylesheet" type="text/css" /> <title></title> </head> <body> <form id="form1" runat="server"> <div id="wrapper"> <div id="content" style="margin-left: 0px"> <h2 style="text-align: center; margin-top: 10px;">Error Logs Tracker</h2> <p style="text-align: left"> </p> <br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <br /> <asp:GridView CssClass="" ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting" CellPadding="4" ForeColor="#333333" GridLines="None"> <AlternatingRowStyle BackColor="White" /> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView> <br /> </div> </div> <br /> <br /> </form> </body> </html> aspx.cs code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Data; using System.Diagnostics; namespace CSV_to_Browser { public partial class DisplayTextFile : System.Web.UI.Page { private string SortDirection = "ASC"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string spath = Server.MapPath("~/App_Data/All_2015-01-06.log"); DataTable dt = ConvertCSVtoDataTable(spath); GridView1.DataSource = dt; GridView1.DataBind(); ViewState["tables"] = dt; } } public DataTable ConvertCSVtoDataTable(string strFilePath) { Stream stream = File.Open(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(strFilePath); string[] headers = sr.ReadLine().Split(',' , ']'); DataTable dt = new DataTable(); foreach (string header in headers) { dt.Columns.Add(header); } while (!sr.EndOfStream) { string[] rows = sr.ReadLine().Split(',', ']'); DataRow dr = dt.NewRow(); for (int i = 0; i < headers.Length; i++) { dr[i] = rows[i]; Debug.WriteLine(i.ToString()); } dt.Rows.Add(dr); } return dt; } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { if (ViewState["tables"] != null) { DataTable dt = (DataTable)ViewState["tables"]; //Sort the data. dt.DefaultView.Sort = e.SortExpression + " " + SortDirection; GridView1.DataSource = dt; GridView1.DataBind(); } SetSortDirection(SortDirection); } protected void SetSortDirection(string sortDirection) { if (sortDirection == "ASC") { SortDirection = "DESC"; } else { SortDirection = "ASC"; } } } }
Tuesday, May 24, 2016 3:20 PM
Answers
-
User1559292362 posted
Hi gssingh04,
How can I show the data in GridView in descending order as default?According to your description, you want to show data in Gridview descending order with a column as default. if it's the case, firstly, we need a select a column which could descending order with it. like this:
private string SortDirection = "DESC"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string spath = Server.MapPath("~/App_Data/All_2015-01-06.log"); DataTable dt = ConvertCSVtoDataTable(spath); string sortExpression = "yourcolumnname DESC"; dt.DefaultView.Sort = sortExpression; GridView1.DataSource = dt; GridView1.DataBind(); ViewState["tables"] = dt; } }
For more information, please refer to:
https://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.110).aspx
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 25, 2016 4:48 AM -
User1559292362 posted
Hi gssingh04,
Some error log files do not have column headings and for some files column names are totally different. The code breaks when it is looking for a particular column heading, that is not contained in the log file. How would I get around that?Please retrieve the first columns name via DataTable, like this:
public static string SortDirection = "DESC"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string spath = Server.MapPath("~/GridViewDemo/2016_01_16_TRSi_Transaction_Log.txt"); DataTable dt = ConvertCSVtoDataTable(spath); if(dt.Rows.Count>0 && dt.Columns.Count>0) { string firstColsName = dt.Columns[0].ColumnName; dt.DefaultView.Sort = string.Format("{0} {1}", firstColsName, SortDirection); GridView1.DataSource = dt.DefaultView; GridView1.DataBind(); ViewState["tables"] = dt; } } }
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 28, 2016 2:21 AM
All replies
-
User1559292362 posted
Hi gssingh04,
How can I show the data in GridView in descending order as default?According to your description, you want to show data in Gridview descending order with a column as default. if it's the case, firstly, we need a select a column which could descending order with it. like this:
private string SortDirection = "DESC"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string spath = Server.MapPath("~/App_Data/All_2015-01-06.log"); DataTable dt = ConvertCSVtoDataTable(spath); string sortExpression = "yourcolumnname DESC"; dt.DefaultView.Sort = sortExpression; GridView1.DataSource = dt; GridView1.DataBind(); ViewState["tables"] = dt; } }
For more information, please refer to:
https://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.110).aspx
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 25, 2016 4:48 AM -
User-2055741253 posted
Cole Wu,
Very much appreciated! There is just one dilemma in this particular case. Some error log files do not have column headings and for some files column names are totally different. The code breaks when it is looking for a particular column heading, that is not contained in the log file. How would I get around that?
Sincerely,
Gagan Marwah
Wednesday, May 25, 2016 3:30 PM -
User1559292362 posted
Hi gssingh04,
Some error log files do not have column headings and for some files column names are totally different. The code breaks when it is looking for a particular column heading, that is not contained in the log file. How would I get around that?Please retrieve the first columns name via DataTable, like this:
public static string SortDirection = "DESC"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string spath = Server.MapPath("~/GridViewDemo/2016_01_16_TRSi_Transaction_Log.txt"); DataTable dt = ConvertCSVtoDataTable(spath); if(dt.Rows.Count>0 && dt.Columns.Count>0) { string firstColsName = dt.Columns[0].ColumnName; dt.DefaultView.Sort = string.Format("{0} {1}", firstColsName, SortDirection); GridView1.DataSource = dt.DefaultView; GridView1.DataBind(); ViewState["tables"] = dt; } } }
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 28, 2016 2:21 AM