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

[Solved] Header-Click sorts wrong column

3 Answers 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 30 Jul 2013, 04:42 PM
Hello!

I am hoping for whatever column header i click to then be sorted (Asc,Des,None)
Currently this works on the first header click, but then after selecting a different column, the wrong header is sorted!
Here is my click order:
Click Col 1, Col 1 sorts.
Click Col 2, Col 3 sorts.
Click Col 2, Col 2 sorts.
Click Col 3, Col 4 sorts.
Click Col 3, Col 3 sorts.
Click Col 1, Col 1 sorts!

Any tips on what might be happening would be great! I have a feeling it may be related to my binding causing a loss of header order?

C#:

protected void Page_Init(object sender, EventArgs e)
      {
          if (IsPostBack)
          {
              pogrid.DataSource = null;
              pogrid.Rebind();
          }
          else
          {
              //... set filteredPOList
              pogrid.DataSource = null;
              pogrid.Rebind();
 
 
public void Selection_Changed(object sender, EventArgs e)
      {
          //...
      }
 
      protected void Grid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
      {
          pogrid.DataSource = filteredPOList;
           
      }


ASPX:  
<telerik:RadGrid  ID="pogrid" runat="server" EnableViewState="true" AutoGenerateColumns="False" OnNeedDataSource="Grid_NeedDataSource" AllowFilteringByColumn="True"  AllowSorting="True"
ViewStateMode="Enabled" CellSpacing="0" OnSelectedIndexChanged="Selection_Changed" GridLines="None" >
                <GroupingSettings CaseSensitive="false"  />
                <ClientSettings EnableRowHoverStyle="True">
                <Selecting AllowRowSelect="True" ></Selecting>
                <Scrolling  UseStaticHeaders="True" AllowScroll="True" SaveScrollPosition="True" ScrollHeight="300px">     </Scrolling>
                <Resizing AllowColumnResize="True"></Resizing>
                </ClientSettings>
            <MasterTableView DataKeyNames="poId">
            <Columns>
                <telerik:GridBoundColumn HeaderStyle-Width="20%" HeaderStyle-Font-Bold="true" HeaderText="Promotion Number" UniqueName="PromotionNumber" DataField="PromotionNumber" SortExpression="PromotionNumber"
                  FilterDelay="2000" CurrentFilterFunction="StartsWith" FilterControlWidth="80%" ></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderStyle-Width="40%"  HeaderStyle-Font-Bold="true" HeaderText="Promotion Name" UniqueName="PromotionName" DataField="PromotionName" SortExpression="PromotionName"
                  FilterDelay="2000" CurrentFilterFunction="Contains"  FilterControlWidth="80%" ></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderStyle-Width="20%"  HeaderStyle-Font-Bold="true" HeaderText="Pep Contact" UniqueName="PepContact" DataField="PepContact" SortExpression="PepContact"
                  FilterDelay="2000" CurrentFilterFunction="Contains" FilterControlWidth="80%" ></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderStyle-Width="20%"  HeaderStyle-Font-Bold="true" HeaderText="PO Name" UniqueName="POName" DataField="POName" SortExpression="POName"
                  FilterDelay="2000" CurrentFilterFunction="Contains" FilterControlWidth="80%" ></telerik:GridBoundColumn>
            </Columns>
            </MasterTableView>
    </telerik:RadGrid>

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ajaxsettings>
            <telerik:AjaxSetting AjaxControlID="pogrid">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="pogrid" LoadingPanelID="RadAjaxLoadingPanel1">
                    </telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </ajaxsettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>

3 Answers, 1 is accepted

Sort by
0
Joe
Top achievements
Rank 1
answered on 30 Jul 2013, 04:44 PM
Also i have some ajax tied to the grid..

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ajaxsettings>
            <telerik:AjaxSetting AjaxControlID="pogrid">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="pogrid" LoadingPanelID="RadAjaxLoadingPanel1">
                    </telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </ajaxsettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>


My main goal is to get a grid that is sortable, filterable, 4-Column grid going, that upon clicking a row, I can access a button that relates to the data in that row. (I already have everything working except this sorting problem)
0
Deyan Enchev
Telerik team
answered on 02 Aug 2013, 11:25 AM
Hello Joe,

I was able to reproduce the issue you are experiencing. You are right that the binding used is causing the wrong sorting of columns.

In order to avoid this behavior you have two options:

  • Instead of rebinding the Grid on Page_Init move the code under Page_Load method;
  • If you do not need to call rebinding code explicitly on every postback, it is good to have in mind that RadGrid supports advanced data binding. As our documentation states, RadGrid will bind to its data source any time it needs data: "The key to the advanced data binding of a RadGrid control is handling the NeedDataSource event. RadGrid fires the NeedDataSource event each time it needs to be bound to a data source."

In our help topic you can read more about the RadGrid's advanced data-binding and why it is not necessary to bind the RadGrid using custom code.

I hope this helps.

Regards,
Deyan Enchev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Joe
Top achievements
Rank 1
answered on 02 Aug 2013, 01:06 PM
Deyan, Thanks for looking into my issue!

I was able to get the grid working by starting from scratch.. I no longer have the Page_Init code, and my grid uses the NeedDataSource function. Thanks!!
Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Joe
Top achievements
Rank 1
Deyan Enchev
Telerik team
Share this question
or