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

RadGrid built dynamically error when changing column order

3 Answers 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Kucharski
Top achievements
Rank 1
David Kucharski asked on 19 Sep 2013, 07:50 PM
I am building my radgrid dynamically from the Page_Init of my form. The PageInit calls the procedure defineGridStructure. Below is the code for that prodedure. Near the top of the procedure you will see me call WB_sp_RetrieveGeographySelectionColumnOrder store procedure. This gets me the columns in the order the user wants to see them. In the Do While loop, I create the columns in the order the user wants them and with the attributes needed. (columnCreation is the procedure that does this). This RadGrid gets its information using the NeedDataSource and ItemDataBound functions for the Grid. When the screen populates the grid works great, column order is what the user wants, and data is retrieved.

Here is the problem: While the user is on the form, they can pop-up another form which allows them to set the columns in different order. Once they save that, they can then come back to the original form, click a submit button and the screen should refresh with the new order of columns. Stepping through the code, my defineGridStructure runs fine, calls the Stored Procedure to get the new column order and applies the new order. It then goes to the NeedDataSource and ItemDataBound respectively. Inside the ItemDataBound is where the issue lies. Once I get my e.Item (GridDataItem)  dataRow, I see the the information is still being bound to the columns in the original order. I get errors because of the different type issue.

example: in the original order, the first column was gridtemplatecolumn for checkbox, and second was a string. Once I switched the two in the pop-up, and hit the submit, the first column is a GridBoundColumn for string, the second column is gridtemplatecolumn for checkbox but the information bound to the row is still looking at it like the original order.

It seems to be something about after changing the order of columns, I need to leave the screen totally, come back in (the system will run the same code) but this time the binding of data is correct. It errors when I have the grid up already, make some changes in order of columns in my database table, then re-submit the page to pick up the new changes. Maybe a session thing. What do I need to do?

defineGridStructure procedure
RadGrid1.ID = "RadGrid1"
        'RadGrid1.Width = Unit.Pixel(1500)
        RadGrid1.Height = Unit.Pixel(700)
  
        RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace
        RadGrid1.AllowPaging = True
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
        RadGrid1.AutoGenerateColumns = False
        RadGrid1.ShowStatusBar = True
        RadGrid1.AllowSorting = True
        RadGrid1.AllowFilteringByColumn = True
        RadGrid1.MasterTableView.NoDetailRecordsText = "No records could be found."
        RadGrid1.MasterTableView.NoMasterRecordsText = "No records could be found."
        RadGrid1.MasterTableView.ShowHeadersWhenNoRecords = True
  
        RadGrid1.Skin = "WebBlue"
        RadGrid1.HeaderStyle.CssClass = "RadGridHeader"
        RadGrid1.MasterTableView.ItemStyle.CssClass = "DetailsRow1"
        RadGrid1.MasterTableView.AlternatingItemStyle.CssClass = "DetailsRow2"
        RadGrid1.GroupingSettings.CaseSensitive = False
        RadGrid1.ClientSettings.ClientEvents.OnGridCreated = "GetGridObject"
        RadGrid1.ClientSettings.Scrolling.FrozenColumnsCount = 2
        RadGrid1.ClientSettings.AllowColumnsReorder = True
        RadGrid1.ClientSettings.ColumnsReorderMethod = GridClientSettings.GridColumnsReorderMethod.Reorder
        RadGrid1.ClientSettings.Selecting.AllowRowSelect = True
        RadGrid1.ClientSettings.Resizing.AllowColumnResize = True
        RadGrid1.ClientSettings.Scrolling.AllowScroll = True
        RadGrid1.ClientSettings.Scrolling.UseStaticHeaders = True
        RadGrid1.ClientSettings.Scrolling.SaveScrollPosition = True
        RadGrid1.ClientSettings.ClientEvents.OnFilterMenuShowing = "filterMenuShowing"
        RadGrid1.FilterMenu.OnClientShowing = "MenuShowing"
          
        If ConfigurationManager.AppSettings("geoSelectionGridSize") <> "" Then
            RadGrid1.MasterTableView.PageSize = ConfigurationManager.AppSettings("geoSelectionGridSize")
        Else
            RadGrid1.MasterTableView.PageSize = 50
        End If
  
        RadGrid1.MasterTableView.AllowMultiColumnSorting = True
        RadGrid1.MasterTableView.DataKeyNames = New String() {"DetailID", "Day", "FoundGeographies", "SubZipCount"}
  
        RadGrid1.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerBind
  
        RadGrid1.MasterTableView.HierarchyDefaultExpanded = True
  
        Dim dpCounter As Integer = 1
        Dim dpCollection As IList(Of RadListBoxItem) = rlbDistributionPatterns.CheckedItems
        For Each item As RadListBoxItem In dpCollection
            Dim columnGroup As New GridColumnGroup
            columnGroup.HeaderText = item.Text
            columnGroup.Name = "GroupDP" & dpCounter.ToString
            columnGroup.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            RadGrid1.MasterTableView.ColumnGroups.Add(columnGroup)
            dpCounter += 1
        Next
  
        Dim strSQL As String
        Dim columnHeader As String = ""
        Dim columnName As String = ""
        Try
            strSQL = "EXEC WB_sp_RetrieveGeographySelectionColumnOrder " & UcHeader.UserId
            mobjSqlDataReaderColumnheaders = mobjDatabase.fnRetrieveData("DATAREADER", strSQL, "Columns")
            Do While mobjSqlDataReaderColumnheaders.Read
                columnHeader = mobjSqlDataReaderColumnheaders("ColumnHeader").ToString.Trim
                columnName = mobjSqlDataReaderColumnheaders("ColumnName").ToString.Trim
  
                If (columnName = "LocationMarket" Or columnName = "LocationNumber" Or columnName = "LocationName" Or columnName = "Distance" Or _
                    columnName = "Sales" Or columnName = "TotalSales" Or columnName = "BOS" Or columnName = "CumeBOS" Or columnName = "SPH" Or columnName = "SPHIndex") Then
                    If miZipCodeStoreAssociationTypeID > 0 Then
                        columnCreation(columnHeader, columnName, RadGrid1)
                    End If
                ElseIf Left(columnName, 2) = "CD" Then
                    If Right(columnName, 1) = 1 And mstrClientDataCategoryID1Description.Trim <> "" Then
                        columnCreation(mstrClientDataCategoryID1Description.Trim, "CD1", RadGrid1)
                    End If
                    If Right(columnName, 1) = 2 And mstrClientDataCategoryID2Description.Trim <> "" Then
                        columnCreation(mstrClientDataCategoryID2Description.Trim, "CD2", RadGrid1)
                    End If
                    If Right(columnName, 1) = 3 And mstrClientDataCategoryID3Description.Trim <> "" Then
                        columnCreation(mstrClientDataCategoryID3Description.Trim, "CD3", RadGrid1)
                    End If
                ElseIf Left(columnName, 11) = "Demographic" Then
                    If Right(columnName, 1) = 1 And mstrReportDemographicID1Description.Trim <> "" Then
                        columnCreation(mstrReportDemographicID1Description.Trim, "Demographic1", RadGrid1)
                    End If
                    If Right(columnName, 1) = 2 And mstrReportDemographicID2Description.Trim <> "" Then
                        columnCreation(mstrReportDemographicID2Description.Trim, "Demographic2", RadGrid1)
                    End If
                    If Right(columnName, 1) = 3 And mstrReportDemographicID3Description.Trim <> "" Then
                        columnCreation(mstrReportDemographicID3Description.Trim, "Demographic3", RadGrid1)
                    End If
                    If Right(columnName, 1) = 4 And mstrReportDemographicID4Description.Trim <> "" Then
                        columnCreation(mstrReportDemographicID4Description.Trim, "Demographic4", RadGrid1)
                    End If
                    If Right(columnName, 1) = 5 And mstrReportDemographicID5Description.Trim <> "" Then
                        columnCreation(mstrReportDemographicID5Description.Trim, "Demographic5", RadGrid1)
                    End If
                ElseIf (columnName = "Forced" Or columnName = "Version" Or columnName = "SubVersionCategory" Or columnName = "DeliveryCode" Or columnName = "DistributionPatterns") Then
                    If UcHeader.objSession.ApplicationId <> CInt(ConfigurationManager.AppSettings("MediaviewerAppID")) Then
                        If columnName = "Forced" Then
                            columnCreation(columnHeader, columnName, RadGrid1)
                        End If
                        If columnName = "Version" And miVersionsCategoryID > 0 Then
                            columnCreation(columnHeader, columnName, RadGrid1)
                        End If
                        If columnName = "SubVersionCategory" And miVersionsCategoryID > 0 Then
                            columnCreation(columnHeader, columnName, RadGrid1)
                        End If
                        If columnName = "DeliveryCode" And miDeliveryCodeCount > 0 Then
                            columnCreation(columnHeader, columnName, RadGrid1)
                        End If
  
                        If columnName = "DistributionPatterns" Then
                            dpCounter = 1
                            For Each item As RadListBoxItem In dpCollection
                                columnCreation(item.Text, "DP" & dpCounter.ToString, RadGrid1)
                                columnCreation(item.Text, "CoverageDP" & dpCounter.ToString, RadGrid1)
                                dpCounter += 1
                            Next
                        End If
                    End If
                Else
                    columnCreation(columnHeader, columnName, RadGrid1)
                End If
            Loop
            mobjSqlDataReaderColumnheaders.Close()
            mobjSqlDataReaderColumnheaders = Nothing
        Catch exp As Exception
            Throw New Exception(exp.Message & ", frmGeographySelection.defineGridStructure")
        End Try
  
        columnCreation("Geography", "Geography2", RadGrid1)
        columnCreation("Circ Type", "CirculationTypeDescription2", RadGrid1)
  
        columnCreation("FoundGeographies", "FoundGeographies", RadGrid1)
  
        RadGrid1.Width = Unit.Pixel(widthOfRadGridDetails + 40)
  
        Me.PlaceHolder1.Controls.Add(RadGrid1)

3 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 24 Sep 2013, 08:56 AM
Hi,

The grid persists the column order in the ViewState and restores it in the Page_Load event. One option to avoid this behavior is to turn off the ViewState of the control (RadGrid1.EnableViewState = false) or the ViewState of the columns (RadGrid1.MasterTableView.EnableColumnsViewState = false). Note that in this case no settings for the columns will be persisted upon postback, but this should not cause problems since you recreate the entire control on postback in the Page_Init event, so no information needs to be persisted in this case.

Regards,
Marin
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
David Kucharski
Top achievements
Rank 1
answered on 24 Sep 2013, 01:05 PM
I would still like to have the column order persist because the user can still change the order of columns on the page to sort of 'overwrite' what they have saved. Maybe for one time only, they would like to move the columns around to positions that are different from what is saved. In that case I would like to hold those changes after the page is submitted.

So I guess is there a way to keep viewstate on (for when the user actually moves the columns on the grid) but in certain cases (when the user wants to click a button and re-set the grid to the default column position) take viewstate off or have it not apply?
0
Marin
Telerik team
answered on 26 Sep 2013, 11:00 AM
Hello,

 If the ViewState is enabled it will persist the column order in all cases (not just changes by the user). Also you cannot turn the ViewState on or off on demand - it will cause problems within the framework and is not supported.
Another option is to keep the ViewState disabled but manually persist the desired column order (for example in Session) and then load it back when necessary. 
However this is not a trivial task because when the user changes the order of the columns on the client (dragging the column headers to reorder) you will have to manually track this new order and send it back to the server so that it is available in the Page_Init event when you create the control.
There may cause further problems because you will actually end up with two types of reordering - both initiated by the user (one coming from the database and another one from the client-side column reordering of the control) where both will have to be applied in the Page_Init event and one should take precedence over the other - which might lead to a confusing user experience.
That's why it will be easier to maintain and achieve only one type of reordering.

Regards,
Marin
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
David Kucharski
Top achievements
Rank 1
Answers by
Marin
Telerik team
David Kucharski
Top achievements
Rank 1
Share this question
or