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

Basic sort not working

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rhonda
Top achievements
Rank 1
Rhonda asked on 09 Sep 2011, 08:59 PM
Hi all,

This is the first time I have used Telerik RadGrid and I am having trouble getting the basic sort to work.  I have a 5 column grid that is bound to an Entity Framework Entity.  I will post all my code below.  What is happening is, I click on the column header, I can see it go to the DelegateGrid_SortCommand code and the sortexpression and oldsortorder values look correct but after stepping out of the code the column is still not sorted.  BTW.   I got this code from the sample on the website.

I am not sure what I am missing.  I have 3 different grids on this page that will all need to have sort working.

Thanks in advance you for your help.

Rhonda

ASP Page code
<asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager>
  
 <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="DelegateGrid">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="DelegateGrid" LoadingPanelID="RadAjaxLoadingPanel1"/>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
        </telerik:RadAjaxLoadingPanel>
  
   <asp:Panel ID="pnlDelegateMain" runat="server" Visible="true">
       <asp:Panel ID="pnlDelegate" runat="server" CssClass="roundedPanel" Visible="true">
            <div style="padding: 0 0 10px 20px; text-align: left">
                <asp:Label ID="pnlDelegateText" Font-Size="Small" Text="My Delegates" runat="server"></asp:Label>
            </div>
        </asp:Panel>
       <telerik:RadGrid ID="DelegateGrid" runat="server" Skin="WF" EnableEmbeddedSkins="false" AllowSorting="true" OnSortCommand="DelegateGrid_SortCommand" OnItemDataBound="DelegateGrid_ItemDataBound"  Visible="true" >
            <MasterTableView Width="100%" CommandItemDisplay="Bottom" AutoGenerateColumns="false">
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Delegate Name" UniqueName="Name" SortExpression="Name"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Apps" HeaderText="Applications" UniqueName="Apps"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="LastVerified" HeaderText="Last Verified" UniqueName="LastVerified" SortExpression="LastVerified"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ActionRequired" HeaderText="Action Required" UniqueName="ActionRequired" SortExpression="ActionRequired"></telerik:GridBoundColumn>
                    <telerik:GridHyperLinkColumn HeaderText = "Edit Delegate" DataTextField="Name" ItemStyle-HorizontalAlign="Left" UniqueName="EditDelegate" HeaderStyle-Font-Bold="false" HeaderStyle-Wrap="false" DataNavigateUrlFields="Id" DataNavigateUrlFormatString="Delegate.aspx?Id={0}" HeaderStyle-Width="150px"></telerik:GridHyperLinkColumn>
                </Columns>
            </MasterTableView>
            <HeaderStyle BackColor="#666666" Font-Names="verdana, arial" Font-Size="Small" Height="20px" />
        </telerik:RadGrid>
        <asp:Panel ID="pnlAddDelegate" runat="server" Visible="true">
            <div class="portalPanel">
                <asp:HyperLink ID="AddDelegate" runat="server" NavigateUrl="~/Delegate.aspx" Text="+ Add New Delegate"></asp:HyperLink>
            </div>
        </asp:Panel>
        <asp:RoundedCornersExtender ID="RoundedCornersExtender1" runat="server"
                BehaviorID="RoundedCornersBehavior1"
                TargetControlID="pnlDelegate"
                Radius="10"
                Corners="TopLeft, TopRight" />
                <br /><br />
    </asp:Panel>



Code behind.

public List<Entities.Delegate> delegates;
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
       PopulateDelegatesGrid();
   }
}
  
  
public void PopulateDelegatesGrid()
        {
           delegates = BusinessUtility.GetDelegatesByManager("0");
  
            DelegateGrid.DataSource = delegates;
            DelegateGrid.DataBind();
        }
  
  
protected void DelegateGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem cmditm = (GridCommandItem)e.Item;
  
                cmditm.Visible = false;
                //hide add new button
                cmditm.FindControl("InitInsertButton").Visible = false;//hide the text
                cmditm.FindControl("AddNewRecordButton").Visible = false;//hide the image
  
                //hide Refresh button
                cmditm.FindControl("RefreshButton").Visible = false;//hide the text
                cmditm.FindControl("RebindGridButton").Visible = false;//hide the image
            }
  
            if (e.Item is GridDataItem)
            {
                GridDataItem dataBoundItem = e.Item as GridDataItem;
                string Name = dataBoundItem["Name"].Text;
                  
                foreach (Entities.Delegate d in delegates)
                {
                    if (Name == d.Name)
                    {
                        string AppList = string.Empty;
  
                        var applicationsNew = d.Applications;
  
                        foreach (ApplicationBase a in applicationsNew)
                        {
                            AppList = AppList + a.Name + " (" + a.WamId + ")</br>";
                            dataBoundItem["Apps"].Text = AppList;
                        }
                    }
                }
            }
        }
  
        protected void DelegateGrid_SortCommand(object source, GridSortCommandEventArgs e)
        {
            GridSortExpression sortExpression = new GridSortExpression();
            switch (e.OldSortOrder)
            {
                case GridSortOrder.None:
                    sortExpression.FieldName = e.SortExpression;
                    sortExpression.SortOrder = GridSortOrder.Descending;
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpression);
                    break;
                case GridSortOrder.Ascending:
                    sortExpression.FieldName = e.SortExpression;
                    sortExpression.SortOrder = DelegateGrid.MasterTableView.AllowNaturalSort ? GridSortOrder.None : GridSortOrder.Descending;
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpression);
                    break;
                case GridSortOrder.Descending:
                    sortExpression.FieldName = e.SortExpression;
                    sortExpression.SortOrder = GridSortOrder.Ascending;
                    e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpression);
                    break;
            }
  
            e.Canceled = true;
            DelegateGrid.Rebind();
  
        }

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Sep 2011, 11:02 AM
Hello,

please use Grid / Advanced Data Binding for binding the your grid.
Because its needed to bind again datasource to grid after sorting.

or
try below code

protected void DelegateGrid_SortCommand(object source, GridSortCommandEventArgs e)
  {
   ................
   .....................
      e.Canceled = true;
       PopulateDelegatesGrid();
  
  }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Rhonda
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or