Answered by:
asp:gridview sorting the filesize column

Question
-
User1132714760 posted
Hi
I am sorting the file size column in asp:gridview but it is not working correctly.
can some one share the code.
Thanks.
Tuesday, February 12, 2019 9:50 PM
Answers
-
User-1174608757 posted
Hi TejasviRebba,
If file size has different units,you could firstly get the units and switch it in different case . For instance, if unit is "KB", you could change data as 1024"B" which is same to "MB" or "GB". Here is the demo , I hope it could help you.
Aspx:
html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="margin-right: 2px" AllowSorting="true" OnSorting="GridView1_Sorting"> <Columns> <asp:BoundField DataField="FileName" HeaderText="FileName" /> <asp:BoundField DataField="FileSize" HeaderText="FileSize" SortExpression="FileSize"/> </Columns> </asp:GridView> </div> </form> </body> </html>
aspx.cs:
public partial class Grid : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select * from myTable"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { switch (e.SortExpression) { case "FileSize": if (e.SortDirection == SortDirection.Ascending) { string sql = "select * from myTable order by FileSize Asc"; DataTable datatable = SqlHelper.ExecuteDataTable(sql); datatable = datatable.AsEnumerable().OrderBy(f => { string a = f["fileSize"] as string; int a1 = a.LastIndexOf("B");// get the index of 'B' string a2 = a.Substring(a1 - 1); if (a2 == "KB") { int KB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b1 = KB * 1024; return b1; } else if (a2 == "MB") { int MB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b2 = MB * 1024 * 1024; return b2; } else if (a2 == "GB")// since the number is too large, you should use long { long GB = Convert.ToInt64(a.Substring(0, a1 - 1)); long b3 = GB * 1024 * 1024 * 1024; return b3; } else { int B = Convert.ToInt32(a.Substring(0, a1)); return B; } }).CopyToDataTable(); GridView1.DataSource =datatable; GridView1.DataBind(); } else { string sql = "select * from myTable order by FileSize Desc"; DataTable datatable = SqlHelper.ExecuteDataTable(sql); datatable = datatable.AsEnumerable().OrderBy(f => { string a = f["fileSize"] as string; int a1 = a.LastIndexOf("B"); string a2 = a.Substring(a1-1); if (a2 == "KB") { int KB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b1 = KB * 1024; return b1; } else if (a2 == "MB") { int MB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b2 = MB * 1024 * 1024; return b2; } else if (a2 == "GB") { long GB = Convert.ToInt64(a.Substring(0, a1 - 1)); long b3 = GB * 1024 * 1024 * 1024; return b3; } else { int B = Convert.ToInt32(a.Substring(0, a1)); return B; } }).CopyToDataTable(); GridView1.DataSource = datatable; GridView1.DataBind(); } break; // case statements for your other fields. } } }
You could see as below:
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 15, 2019 2:51 AM
All replies
-
User-1174608757 posted
Hi TejasviRebba,
file size columnAccording to your description, Could you please tell me the content you saved in this column and which type do you define?
Else I guess there may exist character like 'kb' in your file size column , since the default sort of Gridview could only depend on the complete number or complete letter of data , I suggest you to use Linq to get the number part of the file size column. Here is the demo ,I hope it could help you.
My datatable shows as below:
Aspx:
<head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="margin-right: 2px" AllowSorting="true" OnSorting="GridView1_Sorting"> <Columns> <asp:BoundField DataField="FileName" HeaderText="FileName" /> <asp:BoundField DataField="FileSize" HeaderText="FileSize" SortExpression="FileSize"/> </Columns> </asp:GridView> </div> </form> </body> </html>
public partial class Grid : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) {
//bind gridview string sql = "select * from myTable"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { switch (e.SortExpression) { case "FileSize": if (e.SortDirection == SortDirection.Ascending) { string sql = "select * from myTable order by FileSize Asc";// sort Ascend DataTable datatable = SqlHelper.ExecuteDataTable(sql); datatable = datatable.AsEnumerable().OrderBy(f => { string a = f["fileSize"] as string; // get the value of fileSize int a1 = a.LastIndexOf("KB");// get the index of 'KB' int a2 = Convert.ToInt32(a.Substring(0,a1));// get the number part of column return a2; }).CopyToDataTable(); GridView1.DataSource =datatable; GridView1.DataBind(); } else { string sql = "select * from myTable order by FileSize Desc"; DataTable datatable = SqlHelper.ExecuteDataTable(sql); datatable = datatable.AsEnumerable().OrderBy(f => { string a = f["fileSize"] as string; int a1 = a.LastIndexOf("KB"); int a2 = Convert.ToInt32(a.Substring(0, a1)); return a2; }).CopyToDataTable(); GridView1.DataSource = datatable; GridView1.DataBind(); } break; } } }it shows as below:
Best Regards
Wei Zhang
Wednesday, February 13, 2019 9:19 AM -
User1132714760 posted
what if file size in Bytes, KB,MB,GB.
Thursday, February 14, 2019 6:56 PM -
User-1174608757 posted
Hi TejasviRebba,
If file size has different units,you could firstly get the units and switch it in different case . For instance, if unit is "KB", you could change data as 1024"B" which is same to "MB" or "GB". Here is the demo , I hope it could help you.
Aspx:
html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="margin-right: 2px" AllowSorting="true" OnSorting="GridView1_Sorting"> <Columns> <asp:BoundField DataField="FileName" HeaderText="FileName" /> <asp:BoundField DataField="FileSize" HeaderText="FileSize" SortExpression="FileSize"/> </Columns> </asp:GridView> </div> </form> </body> </html>
aspx.cs:
public partial class Grid : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select * from myTable"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { switch (e.SortExpression) { case "FileSize": if (e.SortDirection == SortDirection.Ascending) { string sql = "select * from myTable order by FileSize Asc"; DataTable datatable = SqlHelper.ExecuteDataTable(sql); datatable = datatable.AsEnumerable().OrderBy(f => { string a = f["fileSize"] as string; int a1 = a.LastIndexOf("B");// get the index of 'B' string a2 = a.Substring(a1 - 1); if (a2 == "KB") { int KB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b1 = KB * 1024; return b1; } else if (a2 == "MB") { int MB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b2 = MB * 1024 * 1024; return b2; } else if (a2 == "GB")// since the number is too large, you should use long { long GB = Convert.ToInt64(a.Substring(0, a1 - 1)); long b3 = GB * 1024 * 1024 * 1024; return b3; } else { int B = Convert.ToInt32(a.Substring(0, a1)); return B; } }).CopyToDataTable(); GridView1.DataSource =datatable; GridView1.DataBind(); } else { string sql = "select * from myTable order by FileSize Desc"; DataTable datatable = SqlHelper.ExecuteDataTable(sql); datatable = datatable.AsEnumerable().OrderBy(f => { string a = f["fileSize"] as string; int a1 = a.LastIndexOf("B"); string a2 = a.Substring(a1-1); if (a2 == "KB") { int KB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b1 = KB * 1024; return b1; } else if (a2 == "MB") { int MB = Convert.ToInt32(a.Substring(0, a1 - 1)); int b2 = MB * 1024 * 1024; return b2; } else if (a2 == "GB") { long GB = Convert.ToInt64(a.Substring(0, a1 - 1)); long b3 = GB * 1024 * 1024 * 1024; return b3; } else { int B = Convert.ToInt32(a.Substring(0, a1)); return B; } }).CopyToDataTable(); GridView1.DataSource = datatable; GridView1.DataBind(); } break; // case statements for your other fields. } } }
You could see as below:
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, February 15, 2019 2:51 AM