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

Add tremplate Columns Dynamically in Radgrid

3 Answers 360 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Kishor
Top achievements
Rank 2
Kishor asked on 19 Jun 2014, 09:00 AM
  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

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 Jun 2014, 09:52 AM
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.
0
Kishor
Top achievements
Rank 2
answered on 19 Jun 2014, 10:26 AM
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



0
Konstantin Dikov
Telerik team
answered on 24 Jun 2014, 07:45 AM
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.

 
Tags
Ajax
Asked by
Kishor
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Kishor
Top achievements
Rank 2
Konstantin Dikov
Telerik team
Share this question
or