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

Sorting/Reordering columns not functioning as expected

3 Answers 279 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 20 Dec 2013, 02:45 PM
When a user changes the layout of the grid (e.g., by column resize or reorder), I set value that's read on page_load.  If the value is 1, I save the grid settings.  This works fine for column resize, but not for column reorder, the value doesn't get updated (it remains 0).  I have the column order client events pointing to the same event as column resize (as you can see below), but the method is never called.  (I put a breakpoint on the client function and it's only called on column resize, not reorder).

Here's the grid definition:

<telerik:RadGrid
    ID="grdHistory"
    runat="server"
    OnItemCreated="grdHistory_ItemCreated"
    OnItemDataBound="grdHistory_ItemDataBound"
    OnNeedDataSource="grdHistory_NeedDataSource"
    OnPreRender="grdHistory_PreRender"
    OnSortCommand="grdHistory_SortCommand"
    OnPageSizeChanged="grdHistory_SaveSettingsOnClose"
    AutoGenerateColumns="true"
    SkinID="worklist"
    AllowCustomPaging="true"
    AllowPaging="true"
    AllowFilteringByColumn="false"
    Width="99%" >
    <HeaderContextMenu
        OnClientItemClicking="GridHeaderContextMenuOnClientItemClicking">
    </HeaderContextMenu>
    <ClientSettings
        ColumnsReorderMethod="Reorder"
        EnablePostBackOnRowClick="false"
        ReorderColumnsOnClient="False"
        AllowColumnsReorder="True"
        AllowKeyboardNavigation="True">
        <Scrolling AllowScroll="true"
            SaveScrollPosition="true"
            UseStaticHeaders="true" />
        <Resizing ResizeGridOnColumnResize="false"
            AllowColumnResize="true"
            EnableRealTimeResize="false"
            AllowResizeToFit="true" />
        <ClientEvents
            OnColumnShown="SaveSettingsOnClose"
            OnColumnHidden="SaveSettingsOnClose"
            OnColumnResized="SaveSettingsOnClose"
            OnColumnClick="SaveSettingsOnClose"
            OnColumnSwapped="SaveSettingsOnClose"
            OnColumnMovingToLeft="SaveSettingsOnClose"
            OnColumnMovingToRight="SaveSettingsOnClose"
            OnColumnMovedToLeft="SaveSettingsOnClose"
            OnColumnMovedToRight="SaveSettingsOnClose"
            OnMasterTableViewCreated="MasterTableViewCreated"
            OnKeyPress="preventInlineEditing" />
    </ClientSettings>
    <MasterTableView
        commanditemdisplay="None"
        enablecolumnsviewstate="false"
        AllowPaging="True"
        AllowSorting="True"
        Width="100%">
        <HeaderStyle Width="200px" />
        <RowIndicatorColumn Visible="False">
        </RowIndicatorColumn>
    </MasterTableView>
</telerik:RadGrid>

And the method definition:

function SaveSettingsOnClose(sender, eventArgs) {
    triggerIsPostBack = true;
    document.getElementById('<%=hSaveSettingsOnClose.ClientID %>').value = "1";
}

Why isn't the SaveSettingsOnClose method being called when I reorder or sort the columns on the grid?

The second issue has to do with Column sort.  When I sort a column, the server OnSortCommand is called, but when I step through the code and check the grid sort expression count, it's always 0 (even though the visual state of the grid shows the proper sort method).  Here's the OnSortCommand and followon StoreSettings methods:

protected void grdHistory_SortCommand(object sender, GridSortCommandEventArgs e)
{
    StoreLastSort();
 
}


private void StoreLastSort()
{
    if (grdHistory.MasterTableView.SortExpressions.Count > 0)
    {
        //Store the column sorted on for when we reload
        foreach (GridSortExpression gse in grdHistory.MasterTableView.SortExpressions)
        {
            string direction = (gse.SortOrder == GridSortOrder.Ascending) ? "ASC" : "DESC";
            UserSettingHelper.SetUserSetting(SortSettingKey, gse.FieldName + "," + direction);
        }
    }
    else
    {
        UserSettingHelper.DeleteUserSetting(SortSettingKey);
    }
}

Why is the .SortExpressions.Count set to 0 even though the grid appears to be sorted properly when looking at it?

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 23 Dec 2013, 03:32 PM
Hello Matt,

Regarding the first issue you are facing with the not firing client-side event, please note that the mentioned event will fire only if you have set the "ReorderColumnsOnClient" property to "true". In your code snippet, since you have set that property to false, it is expected that the event will not fire.

Additionally, when the sorting is enabled for the grid, the client-side OnColumnClick event will not fire. However, you can handle the OnCommand client-side event and if the command name is "Sort", you could call SaveSettingsOnClose():
<ClientEvents
    OnCommand="GridCommand"
    .....
And the JavaScript:
function GridCommand(sender, args) {
    if (args.get_commandName() == "Sort") {
        SaveSettingsOnClose();
    }
}

Now moving to your other issue. Since you are handling the server-side SortCommand event, the SortExpressions collections is not been created yet. Nevertheless, you can handle the server-side PreRender event, where the SortExpressions collection will be available.

Hope that helps.


Regards,
Konstantin Dikov
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
Matt
Top achievements
Rank 1
answered on 08 Jan 2014, 04:50 PM
Thanks, that did the trick...for existing objects.  But when users attempt to create a new object (for which there's no data), I get the following error:

Client-side data-binding with auto-generated columns is not supported! Please declare at least one column for the grid.

If I remove the OnCommand="GridCommand" statement from the .ascx file, the window opens just fine.

The code above is the same as it is now and the ascx.cs code hasn't changed.  The only changes made were the ones recommended in your response (e.g., adding the GridCommand method and OnCommand definition).

What property needs to be set to force loading server side?  (I've checked the list of properties to see where I could force a server-side bind, but nothing jumped out at me.  I also tried altering need data source, bind data such that it's only done for existing objects, but was unsuccessful; I still encountered the error.)
0
Konstantin Dikov
Telerik team
answered on 13 Jan 2014, 02:33 PM
Hi Matt,

The behavior that you are observing is due to the fact that you are not setting correct DataSource on the server-side NeedDataSource event and that you are handling the client-side OnCommand event. The combination of the two will lead to the error in question.

However, when there is no data source to be assigned to the grid, you could use the following line in the NeedDataSource event handler, which should resolve the issue you are facing:
(sender as RadGrid).DataSource = new Dictionary<string, string>();

Hope that helps.


Regards,
Konstantin Dikov
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.
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Matt
Top achievements
Rank 1
Share this question
or