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

Sorting issue with Autogenerated columns

2 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
EmpowerIT
Top achievements
Rank 2
EmpowerIT asked on 04 Nov 2009, 01:30 AM
Hi,
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 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Nov 2009, 10:05 AM
Hi,

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

Thanks
Shinu.
0
EmpowerIT
Top achievements
Rank 2
answered on 04 Nov 2009, 10:11 PM
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
            else 
            { 
                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%;"
                </GroupPanel> 
                <ExportSettings> 
                    <Pdf FontType="Subset" PaperSize="Letter" /> 
                    <Excel Format="Html" /> 
                </ExportSettings> 
                <MasterTableView CommandItemDisplay="Top" CurrentResetPageIndexAction="SetPageIndexToFirst" 
                    Dir="LTR" EditMode="InPlace" Frame="Border" TableLayout="Auto"
                    <EditFormSettings> 
                        <EditColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType"
                        </EditColumn> 
                    </EditFormSettings> 
                    <Columns> 
                        <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:GridButtonColumn> 
                        <telerik:GridEditCommandColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                            UniqueName="AutoGeneratedEditColumn" EditImageUrl="~/Images/icon_edit.gif" ButtonType="ImageButton"
                            <HeaderStyle Width="30px" /> 
                        </telerik:GridEditCommandColumn> 
                    </Columns> 
                    <ExpandCollapseColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                        Resizable="False" Visible="False"
                        <HeaderStyle Width="20px" /> 
                    </ExpandCollapseColumn> 
                    <RowIndicatorColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType" 
                        Visible="False"
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                </MasterTableView> 
                <PagerStyle Mode="NextPrevNumericAndAdvanced" /> 
            </telerik:RadGrid> 

Thanks for your help so far
Tags
Grid
Asked by
EmpowerIT
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
EmpowerIT
Top achievements
Rank 2
Share this question
or