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

Dynamic column in RadGrid doesn't show correct DataFormat when its first load

10 Answers 84 Views
Grid
This is a migrated thread and some comments may be shown as answers.
George
Top achievements
Rank 1
George asked on 21 Aug 2014, 07:33 AM
Hi all,
I have a RadGrid to display all the data from a SqlDataSource which is calling a stored procedure that generate dynamic column name and data.
The first column of the RadGrid is a link column and the following column is generated from the SP.
Due to the dynamic columns, so i cannot pre-set any column setting in the RadGrid such as $ currency sign and thousand separator,
I have to set it during the RadGrid1_ItemDataBound event, but the RadGrid doesn't show the currency format when it first load,
the RadGrid only show the correct currency format after Column Grouping (Ajax).

Anyway to show the correct currency format during the RadGrid first load?

Thanks!
George


<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="false"
            DataSourceID="SqlDataSource1" ShowGroupPanel="True" Font-Size="3pt" Width="950px" Height="600px">
            <ExportSettings FileName="ExportToExcel">
            </ExportSettings>
            <ClientSettings AllowColumnsReorder="false" AllowDragToGroup="True" ReorderColumnsOnClient="True">
                <Scrolling AllowScroll="True" UseStaticHeaders="false" />
                <Resizing AllowColumnResize="false" AllowRowResize="false" ResizeGridOnColumnResize="false"
                    ClipCellContentOnResize="False" EnableRealTimeResize="True" AllowResizeToFit="True" />
            </ClientSettings>
            <MasterTableView DataSourceID="SqlDataSource1">
                <CommandItemSettings ShowRefreshButton="false" ShowExportToCsvButton="True" ShowExportToExcelButton="True" ShowExportToPdfButton="True" ShowExportToWordButton="True" />
                <Columns>
                    <telerik:GridHyperLinkColumn UniqueName="Link" DataNavigateUrlFields="MasterID" DataNavigateUrlFormatString="JavaScript:openwindow('{0}');" Text ="View"
                    </telerik:GridHyperLinkColumn>
                </Columns>
 
            </MasterTableView>
        </telerik:RadGrid>


Code Behind:
Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
 
    If TypeOf e.Item Is GridDataItem AndAlso e.Item.ItemIndex = 0 Then
        Dim dataItem As GridDataItem = TryCast(e.Item, GridDataItem)
        For Each col As GridColumn In dataItem.OwnerTableView.RenderColumns
            Dim numCol As GridNumericColumn = TryCast(col, GridNumericColumn)
            If numCol IsNot Nothing Then
                numCol.DataFormatString = "{0:C2}"
                numCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right
 
            End If
        Next
    End If
 
End Sub



10 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 21 Aug 2014, 08:11 AM
Hi George,

You can try the following code snippet in the PreRender event to set DataFormat for numeric column.

VB:
Private Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
    For Each col As GridColumn In RadGrid1.MasterTableView.RenderColumns
        Dim numCol As GridNumericColumn = TryCast(col, GridNumericColumn)
        If numCol IsNot Nothing Then
            numCol.DataFormatString = "{0:C2}"
            numCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right
        End If
    Next
    RadGrid1.Rebind()
End Sub

Thanks,
Princy
0
George
Top achievements
Rank 1
answered on 26 Aug 2014, 01:56 AM
Hi Princy,
The code snippet works perfectly for my earlier scenario,
but it doesn't work on my 2nd scenario which also calling from the SP, but this time based on the value from the hidden field:

<telerik:RadGrid ID="gvSummary" runat="server" DataSourceID="SqlDataSource1" Width="850px">
                    <MasterTableView DataSourceID="SqlDataSource1">
                    </MasterTableView>
</telerik:RadGrid>
 
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Connection %>" SelectCommand="UDSP_LoadDetailSubsSummary" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:ControlParameter ControlID="hdfMasterId" DefaultValue="" Name="MasterId" PropertyName="Value" Type="Int32" />
                </SelectParameters>
</asp:SqlDataSource>

Code Behind:
Tried at the PreRender, ItemDataBound, ItemCreated, ItemInserted, still same.
But if put the code at the PreRender, it will work after a postback...

Kindly help...
Thanks!




 
0
Princy
Top achievements
Rank 1
answered on 26 Aug 2014, 05:12 AM
Hi George,

You can use the PreRender event. Can you make sure you are rebinding the Grid after making the changes in the PreRender Event so that the changes can be visible. Since you are getting it to work after postback, you are missing a Rebind(). Please try it and if not able to solve provide your code in PreRender event.

Thanks,
Princy
0
George
Top achievements
Rank 1
answered on 26 Aug 2014, 05:42 AM
Hi Princy,

Thanks for your prompt reply.
Indeed I have the Rebind() in the PreRender event,
attached with the full code as below:

ASPX:
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
 
<telerik:RadAjaxPanel ID="ajxPanel" runat="server" LoadingPanelID="RadAjaxLoadingPanel1" >
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ECapExConnection %>" SelectCommand="UDSP_LoadDetailSubsSummary" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="hdfMasterId" DefaultValue="" Name="MasterId" PropertyName="Value" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
            
    <telerik:RadGrid ID="gvSummary" runat="server" DataSourceID="SqlDataSource1" Width="850px">
        <MasterTableView DataSourceID="SqlDataSource1">
        </MasterTableView>
 
    </telerik:RadGrid>
 
</telerik:RadAjaxPanel>


Code Behind:
Private Sub gvSummary_PreRender(sender As Object, e As EventArgs) Handles gvSummary.PreRender
        For Each col As GridColumn In gvSummary.MasterTableView.RenderColumns
            Dim numCol As GridNumericColumn = TryCast(col, GridNumericColumn)
            If numCol IsNot Nothing Then
                numCol.DataFormatString = "{0:C2}"
                numCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right
            End If
        Next
        gvSummary.Rebind()
    End Sub



0
George
Top achievements
Rank 1
answered on 26 Aug 2014, 06:12 AM
Btw, the value of "RenderColumns" is 2, during the PreRender event:
Column 1 is GridExpandColumn
Column 2 is GridRowIndicatorColumn

For Each col As GridColumn In gvSummary.MasterTableView.RenderColumns

0
Princy
Top achievements
Rank 1
answered on 27 Aug 2014, 04:04 AM
Hi George,

I guess you are using AutoGeneratedColumns. Can you try the following code snippet, it works fine at my end.

VB:
Private Sub gvSummary_PreRender(sender As Object, e As EventArgs) Handles gvSummary.PreRender
For Each col As GridColumn In gvSummary.MasterTableView.AutoGeneratedColumns
            Dim numCol As GridNumericColumn = TryCast(col, GridNumericColumn)
            If numCol IsNot Nothing Then
                numCol.DataFormatString = "{0:C2}"
                numCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right
            End If
        Next
        gvSummary.Rebind()
    End Sub

Thanks,
Princy
0
George
Top achievements
Rank 1
answered on 27 Aug 2014, 07:27 AM
Hi Princy,

Yes, the RadGrid is using AutoGeneratedColumns. I have tried the above code snippet,
but still can't get it work at PreRender event, the value of "gvSummary.MasterTableView.AutoGeneratedColumns" is 0.
But if i paste the code into ItemDataBound event, it can get the correct value, column and such,
but it will keep looping into the same event  due to the gvSummary.Rebind() at the end of the line,
until it prompt out the error:

Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space.

If remove the Rebind(), there is not effect for the changes in the RadGrid.
0
Konstantin Dikov
Telerik team
answered on 01 Sep 2014, 05:14 AM
Hello George,

When auto-generated columns are used, within the OnPreRender event handler of the grid you should be able to see all generated GridColumn in the MasterTable.AutoGeneratedColumns collection.

Regarding the issue that you are experiencing if you are calling the Rebind() method within the OnItemDataBound event, this is rather expected and you should not call it within that event handler, because you will get a recursion.

Since the correct way for handling your requirement is to call the Rebind() method on the OnPreRender event, could you please provide the entire markup and code-behind of your grid, so we can investigate what could cause the AutoGeneratedColumns collection to be null.

I am looking forward to your reply.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
George
Top achievements
Rank 1
answered on 02 Sep 2014, 12:06 AM
Hi Konstantin Dikov,
Below ASPX code is from a UserControl

<telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Connection %>" SelectCommand="UDSP_LoadDetailSubsSummary" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="hdfMasterId" DefaultValue="" Name="MasterId" PropertyName="Value" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
      
<telerik:RadGrid ID="gvSummary" runat="server" DataSourceID="SqlDataSource1" Width="850px">
    <MasterTableView DataSourceID="SqlDataSource1">
    </MasterTableView>
</telerik:RadGrid>
 
<asp:HiddenField ID="hdfMasterId" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.MasterId")%>' />

Code Behind:
Private Sub gvSummary_PreRender(sender As Object, e As EventArgs) Handles gvSummary.PreRender
 
    For Each col As GridColumn In gvSummary.MasterTableView.AutoGeneratedColumns
        Dim numCol As GridNumericColumn = TryCast(col, GridNumericColumn)
        If numCol IsNot Nothing Then
            numCol.DataFormatString = "{0:C2}"
            numCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right
        End If
    Next
 
    gvSummary.Rebind()
End Sub


Thanks!
Best regards,
George
0
Konstantin Dikov
Telerik team
answered on 04 Sep 2014, 01:23 PM
Hello George,

I have tested the exact same scenario that you have, with a Stored Procedure and a parameter retrieved from a HiddenField control and I was still not able to replicate the behavior that you are describing. On my end, the MasterTableView's AutoGeneratedColumns collection contains all columns.

Since we are unable to replicate the issue and in order for us to be able to assist you any further on this matter, please open a regular support ticket and attach a sample, runnable project replicating the same problematic behavior. Additionally, please provide information on the exact version of our controls that you are using.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
George
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
George
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or