a ListView, a LinqDataSource, linq-to-sql and ordering - asp.net

So I'm writing a page which does some reporting and it's pretty dynamic, the user has the ability to group data and sort columns. I've been able to get my dynamic grouping down and then the sorting except now my generated linq-to-sql sql order by statement is backwards from what I want it to be. I think I need to figure out how to get at the Result returned from the LinqDataSource when the Sorting event is fired in the ListView so that I can then append my grouping order by clauses.
Currently I'm overriding the LinqDataSource selecting event to provide it's result and do the dynamic grouping there, but then the Result is modified by the Sort on the listview and I was thinking there must be some way to get at that result during the Sorting or Sorted event.
Any insight would be appreciated, in the meantime I will be attempting to figure this out myself.

Awesome I think I found my answer, set AutoSort to false on my LinqDataSource and then sort the data myself, since I'm already sorting myself for the other data.

You can use Linq to manipulate the results of your queries (IEnumerables) based on user inputs. For example (very pseudocode event handler follows):
public void OnUserClickNameColumn(object sender, EventArgs e){
var data = DataProvider.RetrieveAllUsers();
// switch between sorting asc and desc when user clicks on a column head
SessionHelper.CurrentUser.NameColumnSort = !SessionHelper.CurrentUser.NameColumnSort;
if(SessionHelper.CurrentUser.NameColumnSort) // true = sort asc else sort desc
UserDataGrid.DataSource = data.OrderBy(x=>x.Name);
else
UserDataGrid.DataSource = data.OrderByDescending(y=>y.Name);
}

Related

Sorting a Calculated datasource in AppMaker

I have a Calculated datasource that's populated via an appscript function. When I display this datasource in a table, it works great but I can't click on the headers to sort them. I'd like to implement sorting.
I can see that sorting is specified in the Query object using Query.sorting.<fieldname>._ascending() and descending(). What I can't figure out is how to read the value that was set by these functions so I can determine how the user wants me to sort the results.
How do I determine what the desired sort is from the appmaker Query object?
I can't find any documentation around this either, but by inspecting the Query object, I found I can do this:
query.sorting.._order: true - ascending; false - descending; null - no sorting by this column.
query.sorting.._priority: 0 based priority, null - no sorting.
So assuming I have a "Title" column, by inspecting query.sorting.Title._order and query.sorting.Title._priority, I can tell if I should or how to sort by it.

How to disable pivot grid ordering in devexpress?

I am new in devexpress. I have data list to bind pivot grid. And I order my data list from SQL command. But as default pivot grid reoreder my data list to show.
I don't desire to reordered my list. Is it possible to ignore default ordering of pivot grid or some columns?
Source: How to disable default ordering of pivot grid ?
The cause of this behavior is that he pivot has it's own data engine
and has to sort data. Using the provided solution, you can sort field
values in a custom way, for example, using the order from the data
source.
Go through DevExpress thread - Disable Sorting in PivotGrid RowArea to more detailed information about the required functionality.
You can use the ASPxPivotGrid.CustomFieldSort event to sort data manually.
It is necessary to sort data manually by handling the ASPxPivotGrid.CustomFieldSort event. Do your calculation of sorting and grouping at this event. Below, I have posted a code snippet, demonstrating how to disable sorting:
private void pivotGridControl1_CustomFieldSort(object sender, PivotGridCustomFieldSortEventArgs e)
{
e.Result = e.ListSourceRowIndex1.CompareTo(e.ListSourceRowIndex2);
e.Handled = true;
}
References:
ASPxPivotGrid remove custom sort/order

Can my UpdatePanel reference a DataTable without a DB hit everytime it posts back?

I have a paging repeater inside of an UpdatePanel so that I can show, say 10 records at a time of a DataTable. When hitting the next/back buttons it will, of course, show the next 10 or previous 10 records. Is there a way I can have it reference the same DataTable when I hit next/back without having to get the DataTable again from the DB on page load? I think I'm just having a bit of a brain fart. Thanks for the help.
One option is to use .Net's caching APIs. Add your data to the cache for a certain duration or with a dependency, and then retrieve it rather than calling your database query.
http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add.aspx
public void AddItemToCache(Object sender, EventArgs e) {
if (Cache["Key1"] == null)
Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
}
I agree with #TimS but if you have scenario like you want to show the records based on some criteria you need to use ViewState (though it is most discouraged practise) to store the datatable specific to the page's criteria and do the work of next and previous.

Gridview Edit not working after Sorting ASP.NET

I am using C#,ASP.NET
I have a Gridview for which I have provided Sorting, Edit functionality. I am not able to perform EDIT when I perform Sorting. After sorting edit is set on some other row. I think there is some problem with the index it is taking..
Can any one help me how to fix this..
Regards
sbmarya
I think the issue is that the sorting is using a different call/datasouce than the editing. So in the RowEditing event I am getting an index relative to the sort order (either ASC() or DESC()). But then I am binding using getUsers() which is returning the data in a different order.
What I did is I stored some kind of a flag(Value) in ViewState to indicate what sort order I am in and made use of that when I am binding in the Editing event, so that I can call the right method to return the same datasource.
Regards,
sbmarya
I faced this problem as well. This is how I fixed it. (In my example the gridview is sorted on a column called submit date).
a. When the underlying dataTable of the gridview is crated and sorted store it in a session variable. The trick is, before storing to a session variable make sure you store the sorted view.
dt.DefaultView.Sort = "Submit Date" + " " + "DESC";
GridView1.DataSource = dt;
GridView1.DataBind();
Session["gridViewData"] = dt.DefaultView.ToTable(); //Only storing dt will not have the sorted table stored in session.
b. next, perform all edit/update operation using the dataTable stored in session in the above step. It will always come up in proper sorted order and you will not see the issue of row index changing unexpectedly after update.

Apply an ID to a GridView

Before you say it, I'm perfectly aware that a data repeater could do it, but I'm looking for the solution using a GridView.
I'm implementing a jQuery feature and require unique ID's to be applied to individual rows in order to respond to click events in the correct manner. I can't see how you're supposed to apply values to an ID or a row.
Basically I'm looking at doing something along the lines of
$('tr').click(function() {
// get the ID of the tr and redirect the page based on that
});
EDIT: Oh, and I can do it for a row cell if that's the value I want to pass as the query string, however, I don't want to do that as it'd mean displaying a unique identifier which is something the user shouldn't be able to see.
Okay, I didn't expect to actually solve my own question. Just for reference, here it is:
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.ID = e.Row.DataItem("Issue_Key")
End If
which is placed in the RowDataBound event.

Resources