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

GridDropDownColumn ListTextField Sort example

5 Answers 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
ToltingColtAcres asked on 07 Jan 2011, 09:56 PM
I need to be able to sort a RadGrid GridDropDownColumn by the ListTextField and not the ListValueField.

Can someone provide an example on how I can accomplish this?

5 Answers, 1 is accepted

Sort by
0
Mike Nogen
Top achievements
Rank 1
answered on 07 Jan 2011, 10:43 PM
Hello!

Have you tried using the property SortExpression in the GridDropDownColumn 
 SortExpression="CompanyName" instead of the ID/value

/M
0
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
answered on 07 Jan 2011, 10:49 PM
Tried that. I get an error.

The datasource select is  "SELECT Name, Employee_GUID FROM Employees ORDER BY Name"

The griddropdowncolumn definition is:

        <telerik:GridDropDownColumn DataField="Employee_GUID"
            DataSourceID="sqlDataSource_Employees" HeaderText="Employee"
            ListTextField="Name"
            ListValueField="Employee_GUID" UniqueName="Employee_GUID">
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
        </telerik:GridDropDownColumn>


if I add a  SortExpression="Name" to the definition, when I attempt to sort, I get:

 

Server Error in '/VehicleTracking' Application.

Cannot find column Name.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Cannot find column Name.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[IndexOutOfRangeException: Cannot find column Name.]
   System.Data.DataTable.ParseSortString(String sortString) +4988309
   System.Data.DataView.CheckSort(String sort) +37
   System.Data.DataView.set_Sort(String value) +115
   System.Web.UI.WebControls.FilteredDataSetHelper.CreateFilteredDataView(DataTable table, String sortExpression, String filterExpression, IDictionary filterParameters) +118
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +2012
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   Telerik.Web.UI.GridTableView.PerformSelect() +38
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   Telerik.Web.UI.GridTableView.DataBind() +363
   Telerik.Web.UI.GridSortCommandEventArgs.ExecuteCommand(Object source) +325
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +200
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +66
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +142
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
0
Mike Nogen
Top achievements
Rank 1
answered on 08 Jan 2011, 01:47 AM
Sorry,my fault. It wasn´t that easy. Telerik stated on ther help pages that it´s NOT supported in the current version of the Grid.
http://www.telerik.com/help/aspnet/grid/grdfilteringbylisttextfieldforgriddropdowncolumn.html

Depening on you current solution and datasource you can try hooking up an SortCommand (I haven´t tried it) .
http://www.telerik.com/help/aspnet/grid/grdapplycustomsortcriteria.html

protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e)
       {
           if (e.CommandArgument == "Employee_GUID")
           {
               switch (e.OldSortOrder)
               {
                   case GridSortOrder.None:
                       e.Item.OwnerTableView.DataSource =
                         GetDataTable("SELECT FirstName, LastName FROM Employees ORDER BY LEN(FirstName) ASC");
                       e.Item.OwnerTableView.Rebind();
                       break;
                   case GridSortOrder.Ascending:
                       e.Item.OwnerTableView.DataSource =
                         GetDataTable("SELECT FirstName, LastName FROM Employees ORDER BY LEN(FirstName) DESC");
                       e.Item.OwnerTableView.Rebind();
                       break;
                   case GridSortOrder.Descending:
                       e.Item.OwnerTableView.DataSource =
                         GetDataTable("SELECT FirstName, LastName FROM Employees");
                       e.Item.OwnerTableView.Rebind();
                       break;
               }
           }
       }
0
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
answered on 08 Jan 2011, 02:25 AM
Yea, i saw that, was hoping it didn't apply to sorts (since it mentioned filtering).

I tried intercepting the sortcommand (I have an sqldatasource for the source table) but it didn't help.

protected void RadGrid_Vehicles_SortCommand(object sender, GridSortCommandEventArgs e)
{
    if (e.CommandArgument == "Employee_GUID")
    {
        switch (e.NewSortOrder)
        {
            case GridSortOrder.None:
            case GridSortOrder.Ascending:
                SqlDataSource_Employees.SelectCommand = "SELECT Name, Employee_GUID FROM Employees ORDER BY Name ASC";
                SqlDataSource_Employees.Select(DataSourceSelectArguments.Empty);
                e.Item.OwnerTableView.Rebind();
                break;
            case GridSortOrder.Descending:
                SqlDataSource_Employees.SelectCommand = "SELECT Name, Employee_GUID FROM Employees ORDER BY Name DESC";
                SqlDataSource_Employees.Select(DataSourceSelectArguments.Empty);
                e.Item.OwnerTableView.Rebind();
                break;
        }
    }
}

Still sorts by the GUID.

This seems to be a major limitation, I can't imagine why Telerik would sort based on the value and not the text, since what people see is the text and they don't even care what the underlying value (foreign key) is. Seems downright silly to me that you can't do this.
0
Veli
Telerik team
answered on 10 Jan 2011, 05:23 PM
Hi Michael,

That's because the text field in a GriDropDownColumn is not contained in RadGrid's data source. The text field specifies records from another database table related to RadGrid's table by a foreign key specified by the DataField and ListValueField properties. RadGrid's own data table does not contain the DataTextField. You cannot sort of filter by data that is not in the data source of the grid. If you make a JOIN and add the data text field to RadGrid's own data source, then you can specify the field name in the SortExpression property of the 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.
Tags
Grid
Asked by
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
Answers by
Mike Nogen
Top achievements
Rank 1
ToltingColtAcres
Top achievements
Rank 2
Veteran
Iron
Veli
Telerik team
Share this question
or