This is a migrated thread and some comments may be shown as answers.

Sorting Unbound column onneeddatasource

9 Answers 239 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Uri
Top achievements
Rank 1
Uri asked on 28 Jul 2015, 10:51 AM

hey

im using advanced databinding with data from sql db.

so i have this code that when a column (files recieved) is sorted, i highlight rows that a month has passed since the since it was created, files were received but still it was not processed (to alert users). I had the idea i will add an unbound column, and assign it a priority number for the rows that are highlighted. There will be different priority for different "rules", so i could have some rows highlighted red with priority 1, while some are yellow with priority 2. now when this happens i want to create a default sort, before any other sorts are applied, that the rows with highest priority will show up first.

so thats the jist of what im trying to do. i know the regular sort wont work because the item is not bound, so its not a dataitem. what i tried to do was on the onneeddatasource event, after i have my datatable, i apply the sort by priority on the dataview of the datatable (the datatable does have the column with priority with the proper values). This seems to work as the datatable i assign as the datasource for the grid does show up in the exact order it should, priority items first. But i see that the other sort expressions of other columns are not yet applied, this leads me to believe that the datasource gets sorted after the event, overwriting the manual sort i applied. 

is there an event i should override where i can plug in my manual sort? or maybe someone has a better idea how to implement the functionality im looking to achieve? here is the relevant code:

protected void rgListingsReport_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
if ((HttpContext.Current.Cache["data"] == null )
            {
// gets datatable from sql
}​
 else
            {
                _dtReportsSource = (DataTable)HttpContext.Current.Cache["data"];
            } 
            if (_dtReportsSource != null)
            {
                if (doSort) //true when there are rows with priority
                {
                    _dtReportsSource.DefaultView.Sort = "NoticePriority DESC";
                    _dtReportsSource = _dtReportsSource.DefaultView.ToTable();
                    rgListingsReport.DataSource = _dtReportsSource;
                }
                else
                {
                    rgListingsReport.DataSource = _dtReportsSource;
                }
            }
}

on the item databound event i set the priority values for the rows and datatable stored in cache

if (rowColor == Color.Red)
                {
                    dataBoundItem["NoticePriority"].Text = "1";
                    (HttpContext.Current.Cache["data"] as DataTable).Rows[e.Item.RowIndex]["NoticePriority"] = "1";
                }
                else
                {
                    dataBoundItem["rowindex"].Text = "0";
                    dataBoundItem["NoticePriority"].Text = "0";
                }

9 Answers, 1 is accepted

Sort by
0
Uri
Top achievements
Rank 1
answered on 29 Jul 2015, 06:44 PM

bump. anyone? any ideas? i just dont understand why the datasource doesnt update to the modified datatable i assign it. it only changes if i query the DB again, which i want to avoid.

 

thanks!

0
Eyup
Telerik team
answered on 31 Jul 2015, 06:33 AM
Hi Uri,

You should let the grid handle the Sorting operation. I am sending a sample RadGrid web site to demonstrate how you can achieve the requested functionality. Please run the attached application and let me know if it helps you.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Uri
Top achievements
Rank 1
answered on 31 Jul 2015, 01:35 PM
thanks for the reply! ill see if this fits the bill right away and let you know!
0
Uri
Top achievements
Rank 1
answered on 31 Jul 2015, 02:08 PM

Not working for me.

even though when i look at the datatable on the  "RadGrid1.DataSource = AddPriorityField(originalSource);" line the table has the priority row with the right value, once the page loads the priority column comes up as "0" for everything.

one difference i notice is in your sample you "GetGridData" everytime, but i am storing the initial data in httpcontext.current.cache. I am pulling large amounts of data from SQL and dont want to query it every time they sort by the priority column. 

 my feeling is that this is the problem, because if i do re-query from the database it does work. any way around this?

0
Uri
Top achievements
Rank 1
answered on 31 Jul 2015, 02:13 PM

sorry forgot to mention one more difference.

in your code you check the "country" as a rule, for me the rule is dynamic.  and it happens when a checkbox column is filtered, on the itemdatabound event. for example: if the "files received" column is filtered for rows that have that value as true, on the itemdatabound event, i check if 30 days have passed since then AND if the row was "handled", if it wasnt handled i color the row red, and change the value of the "priority" field in the corresponding row in the data table to "1" (thats kept in the httpcontext).

 

i hope you follow, hard to explain :)

btw why cant i "edit" my posts?

 

thanks!

0
Eyup
Telerik team
answered on 05 Aug 2015, 08:20 AM
Hello Uri,

I guess you are keeping your data source in some kind of Session variable after querying it for the first time. You can try to hardcode a new column in this preserved source, similar to the logic in the AddPriorityField(originalSource) method. The key thing here is that on every dynamic condition (checkbox action for instance) you should update the values in this new column hence update the saved source. Thus, when the grid initiates a filter operation and raises its NeedDataSource event, it will get the new updated source.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Uri
Top achievements
Rank 1
answered on 05 Aug 2015, 11:15 AM

hi

yes im keeping the datasource in the httpcontext cache. 

i think i tried what you are saying: 

dtTable.Columns.Add("NoticePriority");
HttpContext.Current.Cache["listingsdata"] = dtTable;
_dtReportsSource = (DataTable)HttpContext.Current.Cache["listingsdata"];​
rgListingsReport.DataSource = _dtReportsSource;

 then when the checkbox filter action is taken, i update "_dtReportsSource" with the new priority values. 

the grid shows up exactly as it should, the noticepriority column has the correct values, but it doesnt sort when i sort by that column. other columns work fine.

aspx of noticepriority column:

<telerik:GridBoundColumn DataField="NoticePriority" UniqueName="NoticePriority" HeaderText="NoticePriority" SortExpression="NoticePriority" Visible="true">

 this is starting to drive me mad hehe. just dont understand what is the problem...

i appreciate if you have any more suggestions!

 

 

 

0
Uri
Top achievements
Rank 1
answered on 05 Aug 2015, 11:18 AM
maybe i should update the values on itemcreated instead of itemdatabound?...
0
Eyup
Telerik team
answered on 07 Aug 2015, 03:01 PM
Hi Uri,

You should update the cached data base on checkbox action - CheckedChanged event handler. And after that call Rebind() to the grid.

If this does not resolve the issue, I suggest that you open a new formal support ticket and send us a very basic runnable web site site, isolated with the grid alone. It would be best if you use a dummy data source, similar to the one used in the attached sample here:
http://www.telerik.com/forums/telerik-grid-with-hyperlink-column#nMNnP8Wb3U28xzCgY7lKRA

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Uri
Top achievements
Rank 1
Answers by
Uri
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or