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

Custom sorting on TemplateColumn

11 Answers 479 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 09 Aug 2011, 02:44 PM
Hello,
I have a DataColumn which contains an item ID and TemplateColumn for that DataColumn, which displays item name depending on ID it's bound to. When I press a header to sort grid, items should sort by names, not by ids. How can I do that?
Almost forgot. Everything I have is a ready-to-use dataset, I cannot manipulate SQL statements and I don't have item names included in that dataset.
Thank you.

11 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Aug 2011, 02:53 PM
Hello,

<telerik:GridTemplateColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                        <ItemTemplate>
                            <%# Eval("ID") %>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
 
// in this sample it shows ID but short by Name.



If above code is not help you then check below link and check GridTemplateColumn in this.
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx

let me know if any concern.
if this is not your case then elaborate your case.

Thanks,
Jayesh Goyani
0
Dmitry
Top achievements
Rank 1
answered on 09 Aug 2011, 03:00 PM
Sorry, I should have clarified something about columns in my first post. They all created runtime, I don't have markup at all. Here is the sample of code-behind which binds column to item and displays the data:
void label_DataBinding(object sender, EventArgs e)
{
            Label lbl = (Label)sender; 
            GridDataItem dataItem = lbl.NamingContainer as GridDataItem;
            DataRowView drv = (DataRowView)dataItem.DataItem;
            if (_refItems.ContainsKey(drv[Item.user_name].ToGuid()))
            {
                lbl.Text = _refItems[drv[Item.user_name].ToGuid()].Caption;
            }
}
And I cannot set SortExpression to "Name" because there's no column "Name" in the dataset.
0
Dmitry
Top achievements
Rank 1
answered on 11 Aug 2011, 11:45 AM
Hello there.
Anyone can help?
0
Veli
Telerik team
answered on 12 Aug 2011, 08:00 AM
Hi Dmitry,

As Jayesh demonstrated, the DataField of your GridTemplateColumn is used for sorting. Alternatively, you can set the SortExpression property if you want to sort by a different data field. In any case, you need to have a column in your DataSet corresponding to the specified field name. RadGrid can only sort by columns in tabular data or by properties in lists of objects. What is your particular scenario? You said you do not have a "Name" column in your data set. What data do you need to sort by?

Greetings,
Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Dmitry
Top achievements
Rank 1
answered on 12 Aug 2011, 08:34 AM
Hello,
Thank you for reply.
There is a code block which I posted above. Column contains an ID, and in DataBinding event template refers to dictionary which contains an item with this ID, and it sets label text to that name. I cannot use regular combobox column because reference data we use is hierarchycal and should be displayed as TreeView in popup window. So we have a column which contains ID and displays Name, and this column should be sorted by Name. That was why I asked if there is a custom sorting for template columns.
1
Accepted
Veli
Telerik team
answered on 12 Aug 2011, 03:28 PM
So, the Name value comes from an external list that is not part of RadGrid's data source. I am afraid RadGrid does not support sorting by data that is outside of its data source.

One option you can take is to use custom sorting in this case. Custom sorting is implemented by handling RadGrid's SortCommand event. You check the command parameters and, if you identify your Name column is to be sorted, cancel the built-in sorting by setting e.Canceled = true. Then you need to sort your data programmatically for this column. I have created a test page to demonstrate this case. The code includes comments to guide you through the logic. Let me know how this works for you.

Greetings,
Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Dmitry
Top achievements
Rank 1
answered on 16 Aug 2011, 09:44 AM
Thank you for reply.
Looks like this is going to work with my project but I cannot implement it fully because I don't have NeedDataSource event ever fired. I create grid entirely in code-behind and it fully consists of template columns. I'm trying to be a good boy building it in Page_Init and calling DataBind() only when !IsPostBack. SortCommand event fires correctly and calls Rebind() after all sorting stuff as in your sample but NeedDataSource still won't fire.
What else this all could be about?
0
Veli
Telerik team
answered on 16 Aug 2011, 11:01 AM
If you are using DataBind(), there is no problem switching to advanced databinding using NeedDataSource event. You handle this event and provide RadGrid's DataSource in it. After switching to NeedDataSource, you do not call DataBind(). RadGrid knows when to rebind and automatically calls the NeedDataSource event when rebinding. When rebinding explicitly, you use Rebind() which, again, calls NeedDataSource. Let me know if something is unclear.

Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Dmitry
Top achievements
Rank 1
answered on 16 Aug 2011, 11:21 AM
Yes I got an idea and now it's working as intended. Thank you very much for help.
And 1 more question. How do I draw a triangle in header template? Well actually I know how and where to draw but how do I access SortExpression and SortOrder from header template?
Thank you.
0
Veli
Telerik team
answered on 16 Aug 2011, 01:12 PM
The NamingContainer of the controls inside your header template (when they are added to the container, of course) is an instance of GridHeaderItem. This item type provides the OwnerTableView property that references the GridTableView containing the item (e.g. the RadGrid.MasterTableView instance). Once you have a reference to the grid table, it's SortExpressions collection give you a list of all the sort expressions applied to the table. You can iterate through this list and check the DataField of the expressions to identify any expressions applying to the current column.

Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Dmitry
Top achievements
Rank 1
answered on 16 Aug 2011, 01:24 PM
Thank you very much.
Tags
Grid
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Dmitry
Top achievements
Rank 1
Veli
Telerik team
Share this question
or