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

radgrid columns at runtime (View and Edit)

1 Answer 270 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chockalingam
Top achievements
Rank 1
Chockalingam asked on 10 Apr 2012, 09:37 PM
Hi,
I am able to create and bind columns at run time into radgrid. But when I click "Edit" link for any record, error message is displayed like this.

Please find below the code for ASP page and C# page.

Thanks,
Chocks

ASP Page

<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Matman.Master" 
    CodeBehind="MaterialView.aspx.vb" Inherits="Matman_WF.MaterialView" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function RowDblClick(sender, eventArgs) {
                sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
            }
        </script>
    </telerik:RadCodeBlock>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="grdMaterial">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="grdMaterial" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
    <div>
        <div style="width:100%; background-color:transparent; font-family:Segoe UI; font-size:medium; font-weight:bold; padding-top:30px;
            margin-bottom:5px; margin-left:3px" >Material View</div>

        <telerik:RadGrid ID="grdMaterial" runat="server" AutoGenerateColumns="false" Height="600px"
            AllowPaging="true" AllowSorting="false" PageSize="200" ShowStatusBar="True" AlternatingItemStyle-BackColor="AliceBlue"
            CellSpacing="0" OnNeedDataSource="grdFieldConfig_NeedDataSource" 
                            OnItemDataBound="grdMaterial_OnItemDataBound" >
            <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
            <GroupingSettings CaseSensitive="false" />
            <AlternatingItemStyle BackColor="AliceBlue"></AlternatingItemStyle>
            <MasterTableView Width="100%" TableLayout="Auto" DataKeyNames="material" 
                             EditMode="InPlace" AllowMultiColumnSorting="false" HeaderStyle-Wrap="true"  >
                <Columns>

                    <telerik:GridEditCommandColumn UniqueName="EditColumn" HeaderStyle-Width="50px" ItemStyle-Width="50px" ></telerik:GridEditCommandColumn>

                    <telerik:GridBoundColumn UniqueName="material" DataField="material"
                        HeaderText = "SAP Material / SKU">
                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="120px" />
                        <ItemStyle HorizontalAlign="Left" />
                    </telerik:GridBoundColumn>

                    <telerik:GridBoundColumn UniqueName="matDescription" DataField="matDescription"
                        HeaderText = "Material Desc">
                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="250px" />
                        <ItemStyle HorizontalAlign="Left" />
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings EnableRowHoverStyle="true" AllowColumnsReorder="true" >
                <Resizing AllowColumnResize="true" EnableRealTimeResize="false" AllowResizeToFit="false" />
                <Scrolling AllowScroll="true" UseStaticHeaders="true" EnableVirtualScrollPaging="false" SaveScrollPosition="false" />
                <Selecting AllowRowSelect="false" />
                <ClientEvents OnRowDblClick="RowDblClick"  />
            </ClientSettings>
        </telerik:RadGrid>
    </div>
</asp:Content>

VB Code (Code Behind)

Imports Telerik.Web.UI

Public Class MaterialView
    Inherits System.Web.UI.Page

    Dim Lst_Columns As List(Of SP_MaterialView_ColumnList_Result) = New List(Of SP_MaterialView_ColumnList_Result)
    Dim Lst_MaterialView As List(Of SP_MaterialView_Result) = New List(Of SP_MaterialView_Result)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not (Page.IsPostBack) Then
                Dim oDB As New MATMAN_Entities()

                'Get list of columns from the database
                Lst_Columns = oDB.SP_MaterialView_ColumnList(1).ToList()

                'Get list of Materials from the database. This is the data the will be displayed in the grid.
                Lst_MaterialView = oDB.SP_MaterialView.ToList()
                Session.Add("Lst_MaterialView", Lst_MaterialView)

                'Bind the list to the grid.
                grdMaterial.DataSource = Lst_MaterialView
                grdMaterial.DataBind()

                If (oDB.Connection.State = ConnectionState.Open) Then
                    oDB.Connection.Close()
                End If

            End If
        Catch ex As Exception
            'Throw ex
        Finally

        End Try

    End Sub

    Protected Sub grdFieldConfig_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdMaterial.NeedDataSource
        Try

            Lst_MaterialView = Session("Lst_MaterialView")
            grdMaterial.DataSource = Lst_MaterialView
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub grdMaterial_OnItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdMaterial.ItemDataBound
        Try

            For Each element As SP_MaterialView_ColumnList_Result In Lst_Columns

                Dim Column As GridBoundColumn = New GridBoundColumn()

                'Check for 1 value in IsEditable field. If present then display that column in grid
                If (element.IsEditable = 1) Then

                    Column.HeaderText = element.Column_DisplayName
                    Column.Display = True
                    Column.DataField = element.ColumnNames
                    Column.UniqueName = element.ColumnNames

                    'Get the value for width property and assign it to the radcolumn. 
                    If (element.Width Is Nothing = False) Then
                        Column.HeaderStyle.Width = Unit.Pixel(element.Width)
                        Column.ItemStyle.Width = Unit.Pixel(element.Width)

                        Column.HeaderStyle.VerticalAlign = VerticalAlign.Top
                        Column.ItemStyle.HorizontalAlign = HorizontalAlign.Left
                    End If

                    grdMaterial.MasterTableView.Columns.Add(Column)

                End If

            Next element

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class


1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 15 Apr 2012, 07:31 PM
Hi Chockalingam,

Note that as our documentation states, RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file. 

Also, columns can be added only during the Init and Load phases of the page. When your grid is declared in mark-up, you can add columns in Page_Load, as shown in the above linked article. 

If the number of columns in your grid is going to change upon postbacks, check this article out as well for some guidelines on how this is best done:
http://www.telerik.com/help/aspnet-ajax/grid-changing-structure-dynamically.html 

If you still have any questions left about this scenario, please post them in the formal support ticket that you have opened on the same problem.

Regards,
Tsvetina
the Telerik team
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 their blog feed now.
Tags
Grid
Asked by
Chockalingam
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or