Sorting issue with Autogenerated columns

3 posts, 0 answers
  1. EmpowerIT
    EmpowerIT avatar
    117 posts
    Member since:
    Jun 2007

    Posted 03 Nov 2009 Link to this post

    I've got a grid that is bound to a bunch of different datasources depending on some other controls on the screen. Due to this, the columns are automatically generated. I have been trying to get the columns to sort, but it doesnt seem to be working.

    I've set the AllowSorting property to true on both the Grid itself and the mastertable view. I can click on the headers, and the sort icons appear, and it triggers a postback, however the data is not being sorted.

    Is there something else that needs to be set up?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Nov 2009 Link to this post


    How do you bind your grid? The inbuilt sorting meachanism of the grid will work properly when you are using the AdvancedDataBinding technique . You can collect more information from the link below:
    Advanced Data-binding

  3. EmpowerIT
    EmpowerIT avatar
    117 posts
    Member since:
    Jun 2007

    Posted 04 Nov 2009 Link to this post

    Hi Shinu,

    I am binding my grid via the NeedDataSource event. I have two button columns defined, and the rest of the columns are created automatically by the rad grid depending on the datasource that is bound to it.
    The scenario I have is that there is a rad grid control and a rad tree view on the page. When a user selects a node on the tree, it causes the grid to rebind to a different datasource based on some information stored on the node.

    In the NeedDataSourceEvent, I do the following:
    void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
            RadGrid grid = RadGrid1; 
            if (grid != null
                // Find the datasource id 
                // if there isn't a valid value, hide the grid 
                string dataSourceId = node.Attributes["DataSourceID"]; 
                if (String.IsNullOrEmpty(dataSourceId)) 
                    grid.Visible = false
                    SqlDataSource datasource = this.Page.FindControl(dataSourceId) as SqlDataSource; 
                    if (datasource != null
                        // Got a valid datasource, now set the datakeynames (primary keys) and datasource 
                        string[] myArray = RadTreeView1.SelectedNode.Attributes["DataKeyNames"].Split(_attrDelim, StringSplitOptions.RemoveEmptyEntries); 
                        grid.Visible = true
                        grid.MasterTableView.DataSource = datasource; 
                        grid.MasterTableView.CanRetrieveAllData = false
                        grid.MasterTableView.DataKeyNames = myArray; 
    Please note that setting the CanRetrieveAllData value to true or false seems to have no effect.

    I call RadGrid1.Rebind() from the node selected event.

    The grid binds correctly, I can insert, update and delete via the Radgrid_Insert, update delete events. I also configure some columns in itemcreated and itemdatabound, but it does not sort correctly. The sorting image appears, and iterates through ascending, descending and natural sort, but the data itself does not sort. I have checked that the sort expressions are specified on the columns, and they are specified correctly.
    I also hide some columns in the RadGrid databound event. These are all the events i capture for the RadGrid:

    RadGrid1.DataBound += new EventHandler(RadGrid1_DataBound); 
    RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource); 
    RadGrid1.UpdateCommand += new GridCommandEventHandler(RadGrid1_UpdateCommand); 
    RadGrid1.DeleteCommand += new GridCommandEventHandler(RadGrid1_DeleteCommand); 
    RadGrid1.InsertCommand += new GridCommandEventHandler(RadGrid1_InsertCommand);
    RadGrid1.ItemDataBound +=new GridItemEventHandler(RadGrid1_ItemDataBound);
    RadGrid1.ItemCreated +=new GridItemEventHandler(RadGrid1_ItemCreated);

    Below is the radgrid in the page:
     <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowPaging="True" 
                    AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" GridLines="None" 
                    Skin="WebBlue" PageSize="30" AllowSorting="true"
                    <GroupPanel ID="GroupPanel" Style="width: 100%;"
                        <Pdf FontType="Subset" PaperSize="Letter" /> 
                        <Excel Format="Html" /> 
                    <MasterTableView CommandItemDisplay="Top" CurrentResetPageIndexAction="SetPageIndexToFirst" 
                        Dir="LTR" EditMode="InPlace" Frame="Border" TableLayout="Auto"
                            <EditColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType"
                            <telerik:GridButtonColumn ButtonType="ImageButton" ImageUrl="~/Images/icon_delete.gif" 
                                CommandName="Delete" CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                                Text="Delete" UniqueName="AutoGeneratedDeleteColumn" ConfirmText="Are you sure you want to delete this record?"
                                <HeaderStyle Width="45px" /> 
                            <telerik:GridEditCommandColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                                UniqueName="AutoGeneratedEditColumn" EditImageUrl="~/Images/icon_edit.gif" ButtonType="ImageButton"
                                <HeaderStyle Width="30px" /> 
                        <ExpandCollapseColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                            Resizable="False" Visible="False"
                            <HeaderStyle Width="20px" /> 
                        <RowIndicatorColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                            <HeaderStyle Width="20px" /> 
                    <PagerStyle Mode="NextPrevNumericAndAdvanced" /> 

    Thanks for your help so far
Back to Top