GridDropDownColumn ListTextField Sort example

6 posts, 0 answers
  1. ToltingColtAcres
    ToltingColtAcres avatar
    83 posts
    Member since:
    Nov 2010

    Posted 07 Jan 2011 Link to this post

    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?
  2. Mike Nogen
    Mike Nogen avatar
    34 posts
    Member since:
    May 2006

    Posted 07 Jan 2011 Link to this post

    Hello!

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

    /M
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ToltingColtAcres
    ToltingColtAcres avatar
    83 posts
    Member since:
    Nov 2010

    Posted 07 Jan 2011 Link to this post

    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
    
  5. Mike Nogen
    Mike Nogen avatar
    34 posts
    Member since:
    May 2006

    Posted 07 Jan 2011 Link to this post

    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;
                   }
               }
           }
  6. ToltingColtAcres
    ToltingColtAcres avatar
    83 posts
    Member since:
    Nov 2010

    Posted 07 Jan 2011 Link to this post

    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.
  7. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 10 Jan 2011 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017