Answered by:
GridView handle onSorting in conde

Question
-
User-131059102 posted
I have the following which displays the data correctly and pagination works good. I however cannot seem to get sorting to work. I only want to sort asc and desc.
Any ideas would be greatly appreciated!
public partial class inLinksAuthGrid : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { RegisterAsyncTask(new PageAsyncTask(LoadSomeData)); } public async Task LoadSomeData() { try { var client = new WebClient(); client.Credentials = new NetworkCredential("test", "test"); var myInLinks = client.DownloadStringTaskAsync("http://inlink-xxxxxx.net:5000/inlinks"); await Task.WhenAll(myInLinks); var links = JsonConvert.DeserializeObject<Rootobject>(await myInLinks); GridView1.DataSource = links.inlinks; GridView1.DataBind(); } catch (Exception ex) { //TODO: } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { // WHAT do I do here? } public class Rootobject { public Inlink[] inlinks { get; set; } } public class Inlink { public string clicks { get; set; } public string email { get; set; } public string ip { get; set; } public string keyword { get; set; } public string timestamp { get; set; } public string title { get; set; } public string url { get; set; } } }
This is the GridView:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="900" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" AllowSorting="True"></asp:GridView>
Tuesday, September 13, 2016 8:23 AM
Answers
-
User283571144 posted
Hi sayitfast,
sayitfast
I have the following which displays the data correctly and pagination works good. I however cannot seem to get sorting to work. I only want to sort asc and desc.As far as I know, we could use GridViewPageEventArgs property value to get the sort message.
SortDirection is sort the grid column using ascending and descending order.
SortExpression is the column name that you need to sort.
dataTable.DefaultView.Sort will sort the data table by assigning sort direction and sort expression to datatable.
So I suggest you could try to use below codes:
private string ConvertSortDirectionToSql(SortDirection sortDireciton) { string newSortDirection = String.Empty; switch (sortDirection) { case SortDirection.Ascending: newSortDirection = "ASC"; break; case SortDirection.Descending: newSortDirection = "DESC"; break; } return newSortDirection } protected void gridView_Sorting(object sender, GridViewSortEventArgs e) { DataTable dataTable = gridView.DataSource as DataTable; if (dataTable != null) { DataView dataView = new DataView(dataTable); dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); gridView.DataSource = dataView; gridView.DataBind(); } }
Note: These codes is worked well with Sqldatasource on my computer.
Since I don't know the type of "links.inlinks", so you could try to use it or do some changes.
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 13, 2016 11:33 AM -
User283571144 posted
Hi sayitfast,
The links.inlinks DataSource is a json file I get in the LoadSomeData() method.As far as I know, JsonConvert.DeserializeObject method could directly convert Json file to Datatable.
I suggest you could use it convert to Datatable, then bind it to GridView.
More details, you could refer to follow codes:
public DataTable DerializeDataTable() { const string json = @"[{""Name"":""AAA"",""Age"":""22"",""Job"":""PPP""}," + @"{""Name"":""BBB"",""Age"":""25"",""Job"":""QQQ""}," + @"{""Name"":""CCC"",""Age"":""38"",""Job"":""RRR""}]"; var table = JsonConvert.DeserializeObject<DataTable>(json); return table; }
This requires the Json.NET framework.
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 14, 2016 1:51 AM
All replies
-
User283571144 posted
Hi sayitfast,
sayitfast
I have the following which displays the data correctly and pagination works good. I however cannot seem to get sorting to work. I only want to sort asc and desc.As far as I know, we could use GridViewPageEventArgs property value to get the sort message.
SortDirection is sort the grid column using ascending and descending order.
SortExpression is the column name that you need to sort.
dataTable.DefaultView.Sort will sort the data table by assigning sort direction and sort expression to datatable.
So I suggest you could try to use below codes:
private string ConvertSortDirectionToSql(SortDirection sortDireciton) { string newSortDirection = String.Empty; switch (sortDirection) { case SortDirection.Ascending: newSortDirection = "ASC"; break; case SortDirection.Descending: newSortDirection = "DESC"; break; } return newSortDirection } protected void gridView_Sorting(object sender, GridViewSortEventArgs e) { DataTable dataTable = gridView.DataSource as DataTable; if (dataTable != null) { DataView dataView = new DataView(dataTable); dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); gridView.DataSource = dataView; gridView.DataBind(); } }
Note: These codes is worked well with Sqldatasource on my computer.
Since I don't know the type of "links.inlinks", so you could try to use it or do some changes.
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 13, 2016 11:33 AM -
User-131059102 posted
The links.inlinks DataSource is a json file I get in the LoadSomeData() method.
I have tried the exact example you mentioned with no luck.
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 + " " + ConvertSortDirectionToSql(e.SortDirection); GridView1.DataSource = dataView; GridView1.DataBind(); } } private string ConvertSortDirectionToSql(SortDirection sortDirection) { string newSortDirection = String.Empty; switch (sortDirection) { case SortDirection.Ascending: newSortDirection = "ASC"; break; case SortDirection.Descending: newSortDirection = "DESC"; break; } return newSortDirection; }
Tuesday, September 13, 2016 5:17 PM -
User283571144 posted
Hi sayitfast,
The links.inlinks DataSource is a json file I get in the LoadSomeData() method.As far as I know, JsonConvert.DeserializeObject method could directly convert Json file to Datatable.
I suggest you could use it convert to Datatable, then bind it to GridView.
More details, you could refer to follow codes:
public DataTable DerializeDataTable() { const string json = @"[{""Name"":""AAA"",""Age"":""22"",""Job"":""PPP""}," + @"{""Name"":""BBB"",""Age"":""25"",""Job"":""QQQ""}," + @"{""Name"":""CCC"",""Age"":""38"",""Job"":""RRR""}]"; var table = JsonConvert.DeserializeObject<DataTable>(json); return table; }
This requires the Json.NET framework.
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 14, 2016 1:51 AM