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

Format AutoGenerated Columns on PostBack

2 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JSaultz
Top achievements
Rank 1
JSaultz asked on 10 May 2010, 10:16 PM
I have a Radgrid that is populated from a Stored Procedure that dynamically creates columns. Therefore, I have created the RadGrid as follows:
  <telerik:RadGrid runat="server" ID="rgMetricsTrending" AutoGenerateColumns="true"
            </telerik:RadGrid> 
Now, the datasource is based on the selected value of a number of controls, but what I need is to change the dataformatString on every column based on the selectedValue of the radComboBox. I attempted to put this in the ColumnCreated sub as follows:
  Private Sub rgMetricsTrending_ColumnCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridColumnCreatedEventArgs) Handles rgMetricsTrending.ColumnCreated 
        Select Case e.Column.UniqueName 
            Case "ExpandColumn" 
            Case "TimeFrame" 
            Case Else 
                Dim col As GridNumericColumn = CType(e.Column, GridNumericColumn) 
                Select Case rcbMetrics.SelectedValue 
                    Case Enums.TrendingMetrics.Abandons, Enums.TrendingMetrics.ACD_Calls, Enums.TrendingMetrics.Calls_Offered 
                        'SET FORMAT TO A NUMBER 
 
                        col.DataFormatString = "{0:g}" 
                    Case Enums.TrendingMetrics.AHT, Enums.TrendingMetrics.AHT_Adj, Enums.TrendingMetrics.ASA 
                        'CAST AND FORMAT AS DATE/TIME 
 
                    Case Enums.TrendingMetrics.Contact_Rate, Enums.TrendingMetrics.Pct_Abandoned, Enums.TrendingMetrics.Pct_Transfer, Enums.TrendingMetrics.Service_Level 
                        'SET FORMAT TO PERCENT W/ 2 DECIMAL PLACES 
                        col.DataFormatString = "{0:p2}" 
 
                End Select 
        End Select 
    End Sub 
The issue that I'm having is that apparently ColumnCreated isn't hit on postback, and I need to know a better place to put the above code snippet. I've tried putting it in the ItemDataBound sub, but I do so using a for each column in rgMetricsTrending.Columns, and the count is always 0, so it never changes. The format changes to what I want, based on the selection, but unfortunately, it doesn't change when selectedValue of the Rad Combo Box changes and rebinds...any help would be greatly appreciated.

2 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 11 May 2010, 04:36 PM
The events you are subscribing to only occur during certain scenarios, such as when the control is initially databound or when rows are being inserted/updated/removed. From the sound of things you might want to subscribe to the "SelectedIndexChanged" event of the RadComboBox and cycle through the columns collection (RadGrid1.MasterTableView.Columns) of the RadGrid. You can also look into the Event sequence of the RadGrid and see if there are any other events you might want to subscribe to. Keep in mind that if you do change the DataFormatString of a column and you are not subscribing to an event that explicitly calls Rebind() you will have to call that function yourself.
0
Veli
Telerik team
answered on 11 May 2010, 05:02 PM
Hello Schlurk,

Also, auto-generated columns are contained in the RadGrid.MasterTableView.AutoGeneratedColumns collection instead. The RadGrid.MasterTableView.Columns collection holds user-defined columns only. So you may want to use:

rgMetricsTrending.MasterTableView.AutoGeneratedColumns

instead.

Best wishes,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
JSaultz
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Veli
Telerik team
Share this question
or