Add tremplate Columns Dynamically in Radgrid

4 posts, 0 answers
  1. Kishor
    Kishor avatar
    82 posts
    Member since:
    Oct 2012

    Posted 19 Jun 2014 Link to this post

      Hello
     I want to generate Radgrid templates column Dynamically on button click event.
    as shown in images attached below  Edit ,Delete and ProductNames columns are Fixed
    I just want to add Country Column dynamically on AddCountry Button click
    Using the following code the new column gets added but the previous column gets deleted , I want to maintain the previous column and the values in it
    as shown it attached images

    Please check if I am missing any thing in the code below
    thanks
    waiting for reply
    Kishor




    Dim
    objProductDataTable As New tblProductDataTable
     Dim objProductTableAdapter As New tblProductTableAdapter
     
       Private Class MyTemplate
           Implements ITemplate
           Protected textBox As TextBox
           Private colname As String
     
           Public Sub New(ByVal cName As String)
               MyBase.New()
               colname = cName
           End Sub
     
           Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
               textBox = New TextBox
               textBox.ID = "abc"
               container.Controls.Add(textBox)
           End Sub
       End Class
     
     
       Protected Sub btnAddCountry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCountry.Click
     
     
           objProductDataTable = objProductTableAdapter.GetProducts()
     
           RadGrid1.DataSource = objProductDataTable
           Dim templateColumnName As String = "Col1"
           Dim templateColumn As New GridTemplateColumn()
           templateColumn.ItemTemplate = New MyTemplate(templateColumnName)
           templateColumn.HeaderText = txtCountry.Text
           templateColumn.ItemStyle.Font.Bold = True
           templateColumn.HeaderStyle.Font.Bold = True
     
           RadGrid1.MasterTableView.Columns.Add(templateColumn)
           RadGrid1.DataBind()
     
     
           For i = 0 To objProductDataTable.Rows.Count - 1
               Dim lbl As Label = DirectCast(RadGrid1.Items(i).FindControl("lblproduct"), Label)
               lbl .Text = objProductDataTable.Rows(i).Item("ProductName")
           Next
     
     
       End Sub
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Jun 2014 in reply to Kishor Link to this post

    Hi Sawan,

    Please have a look into the sample code snippet which works fine at my end. Please provide your full code if it doesn't help.

    ASPX:
    <telerik:RadGrid ID="rgrdOrders" runat="server" DataSourceID="sqldsOrders" AutoGenerateColumns="false">
        <MasterTableView>
            <Columns>
                <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CustomerID" UniqueName="CustomerID">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <asp:Label ID="lblproduct" runat="server">
                        </asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <telerik:RadTextBox ID="rtxtCountry" runat="server">
    </telerik:RadTextBox>
    <telerik:RadButton ID="rbtnAddCountry" runat="server" Text="Add Country" OnClick="rbtnAddCountry_Click">
    </telerik:RadButton>

    VB:
    Private Class MyTemplate
        Implements ITemplate
        Protected textBox As TextBox
        Private colname As String
        Public Sub New(cName As String)
            MyBase.New()
            colname = cName
        End Sub
        Public Sub InstantiateIn(container As System.Web.UI.Control)
            textBox = New TextBox()
            textBox.ID = "abc"
            container.Controls.Add(textBox)
        End Sub
    End Class
    Protected Sub rbtnAddCountry_Click(sender As Object, e As EventArgs)
        Dim templateColumnName As String = "Col1"
        Dim templateColumn As New GridTemplateColumn()
        templateColumn.ItemTemplate = New MyTemplate(templateColumnName)
        templateColumn.HeaderText = rtxtCountry.Text
        templateColumn.ItemStyle.Font.Bold = True
        templateColumn.HeaderStyle.Font.Bold = True
        rgrdOrders.MasterTableView.Columns.Add(templateColumn)
        rgrdOrders.DataBind()
        Dim i As Integer = 0
        For Each item As GridDataItem In rgrdOrders.Items
            Dim lbl As Label = DirectCast(rgrdOrders.Items(i).FindControl("lblproduct"), Label)
            lbl.Text = item("CustomerID").Text
            i += 1
        Next
    End Sub

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kishor
    Kishor avatar
    82 posts
    Member since:
    Oct 2012

    Posted 19 Jun 2014 in reply to Princy Link to this post

    Thanks for the quick reply.
    But my problem is not solved

    I want to add new country column on each time country button is click ,
    New column gets added but the column added previously gets deleted ,
    I want to maintain prevoius columns with there values

    please reply
    thanks




    <
    body>
        <form id="form1" runat="server">
        <div>
         
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <telerik:RadGrid ID="RadGrid1" runat="server">
             
              <MasterTableView CommandItemDisplay="None" AutoGenerateColumns="False" Width="100%">
           
                   <Columns>
                   
                        <telerik:GridButtonColumn HeaderStyle-Font-Bold ="true"  CommandName="UpdateRow" HeaderStyle-Width ="50px"
                                ButtonType="ImageButton"  Text="Edit" ImageUrl ="images/icon/Edit-72.png"
                                UniqueName="EditCommandColumn" HeaderText ="Edit"  >
                                <ItemStyle HorizontalAlign ="Right" />
                        </telerik:GridButtonColumn>
                       
                        <telerik:GridButtonColumn HeaderStyle-Font-Bold ="true" CommandName="DeleteRow" HeaderStyle-Width ="50px" 
                                ButtonType="ImageButton"  Text="Delete"  ImageUrl="images/icon/delete.png" 
                                UniqueName="DeleteColumn" HeaderText ="Delete"  >
                                <ItemStyle HorizontalAlign ="Right" />
                </telerik:GridButtonColumn>
                 
                        <telerik:GridTemplateColumn HeaderText="Product Names"  HeaderStyle-Font-Bold ="true" UniqueName="7" HeaderStyle-Width ="150px" >
                            <ItemTemplate>
                                     <asp:Label ID="lblproduct" runat="server" Font-Size ="12px" Font-Bold ="true" ></asp:Label>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
             
                    </Columns>
            
               </MasterTableView>
             
            </telerik:RadGrid>
                           
            <asp:Button ID="Button1" runat="server" Text="Add Country" Font-Bold ="true" />
                   <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
                   
        </div>
        </form>
    </body>


    Partial Class _Default
        Inherits System.Web.UI.Page

        Dim objProductDataTable As New tblProductDataTable
        Dim objProductTableAdapter As New tblProductTableAdapter

        Private Class MyTemplate
            Implements ITemplate
            Protected textBox As RadNumericTextBox
            Private colname As String

            Public Sub New(ByVal cName As String)
                MyBase.New()
                colname = cName
            End Sub

            Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
                textBox = New RadNumericTextBox
                textBox.ID = "abc"
                container.Controls.Add(textBox)
            End Sub
        End Class


        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

            objProductDataTable = objProductTableAdapter.GetProducts()
            RadGrid1.DataSource = objProductDataTable
            Dim templateColumnName As String = "Col1"
            Dim templateColumn As New GridTemplateColumn()
            templateColumn.ItemTemplate = New MyTemplate(templateColumnName)
            templateColumn.HeaderText = txtCountry.Text
            templateColumn.ItemStyle.Font.Bold = True
            templateColumn.HeaderStyle.Font.Bold = True
            templateColumn.HeaderStyle.Wrap = True

            RadGrid1.Columns.Add(templateColumn)
            RadGrid1.DataBind()

            For i = 0 To objProductDataTable.Rows.Count - 1
                Dim txtid As Label = DirectCast(RadGrid1.Items(i).FindControl("lblproduct"), Label)
                txtid.Text = objProductDataTable.Rows(i).Item("ProductName")
            Next

        End Sub


    End Class



  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 24 Jun 2014 Link to this post

    Hello Sawan,

    First, let me start by saying that 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.

    Additionally, when creating template columns programmatically, the grid must be generated completely in the code-behind using the Page_Init event and column templates must be added in the Page_Init event handler, so that the template controls can be added to the ViewState.

    Please examine the following help articles, which should prove helpful for your requirement:

    Best 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.

     
Back to Top