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

[Solved] GridDataItem in Dynamically created template column

5 Answers 328 Views
Grid
This is a migrated thread and some comments may be shown as answers.
cmehta
Top achievements
Rank 1
cmehta asked on 08 Apr 2009, 01:38 PM
We are creating the radgrid dynamically in code behind with one bound column and other template columns.
We are not able to find checkbox control in gridDataItem using gridDataItem.FindControl method although text of bound column is available.

Please provide solution for the same.

 Code:
The folowing method is called on page load:
----------------------------------------------------------------------------------------------------------------------
Private Function UpdateGridActionColumns() As GridColumnCollection
 Dim actionCollection As GridColumnCollection
        Dim dsActions As New DataSet
        Dim dtActions As New DataTable
        Try
            actionCollection = New GridColumnCollection(RadGrid1_Functions.MasterTableView)
            dsActions = Me._appObject.GetAllActions().ConvertToDataSet()
            RadGrid1_Functions.MasterTableView.Columns.Clear()


            Dim boundColumn As New GridBoundColumn
            boundColumn.DataField = "APP_FUNCTION_ID"
            boundColumn.UniqueName = "SEC_FUNCTION_ID"
            boundColumn.HeaderText = "Function"
            RadGrid1_Functions.MasterTableView.Columns.Add(boundColumn)

            If dsActions.Tables.Count > 0 Then
                dtActions = dsActions.Tables(0)
                For ctr As Integer = 0 To dtActions.Rows.Count - 1
                    Dim templateColumnID As String = dtActions.Rows(ctr)("Action_Id")
                    Dim templateColumn As GridTemplateColumn = New GridTemplateColumn
                    templateColumn.ItemTemplate = New MyTemplate(templateColumnID)
                    templateColumn.UniqueName = templateColumnID
                    templateColumn.DataField = "Action_Id"
                    templateColumn.HeaderText = dtActions.Rows(ctr)("Action_Name")
                    RadGrid1_Functions.MasterTableView.Columns.Add(templateColumn)
                Next
            End If
            Return actionCollection
        Catch ex As Exception
            Return Nothing
        End Try
End Function
----------------------------------------------------------------------------------------------------------------------
Template column class is:

 Private Class MyTemplate
        Implements ITemplate

        Protected chkTempColumn As CheckBox
        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
            chkTempColumn = New CheckBox
            chkTempColumn.ID = "chk" & colname
            AddHandler chkTempColumn.DataBinding, AddressOf Me.chkTempColumn_DataBinding

            Dim table As Table = New Table
            Dim row As TableRow = New TableRow
            Dim cell As TableCell = New TableCell

            table.BorderWidth = 0
            row.BorderWidth = 0
            cell.BorderWidth = 0

            row.Cells.Add(cell)
            table.Rows.Add(row)
            cell.Controls.Add(chkTempColumn)
            container.Controls.Add(table)
            'container.Controls.Add(boolValue)
        End Sub

        Private Sub chkTempColumn_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
            Dim cBox As CheckBox = CType(sender, CheckBox)
            Dim container As GridDataItem = CType(cBox.NamingContainer, GridDataItem)
            Dim dr As DataRow = CType(container.DataItem, DataRowView).Row

            'cBox.Checked = CType(CType(container.DataItem, DataRowView)("Bool"), Boolean)
            Select Case Integer.Parse(dr(colname))
                Case 0
                    cBox.Checked = False
                Case 1
                    cBox.Checked = True
                Case -1
                    cBox.Checked = False
                    cBox.Enabled = False
            End Select
        End Sub
    End Class
----------------------------------------------------------------------------------------------------------------------
Please provide the solution for same.

Please mark a copy of solution to:
chetan.mehta@cognizant.com

Regards
Chetan
Frameworks team, marketRx (A Cognizant company)

P.S: We have purchased Telerik UI suite but raising ticket here since the subscription has expired.

5 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 13 Apr 2009, 06:31 AM
Hello Chetan,

The following demo illustrates how to create RadGrid programmatically on Page.Init(when you have template column you should create your RadGrid on Page.Init)
Grid / On PageInit
and the help article below demonstrates creation of GridTemplateColumn programmatically:
Programmatic creation

Kind regards,
Nikolay
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
cmehta
Top achievements
Rank 1
answered on 15 Apr 2009, 09:39 AM
But we are making the grid in a user control which will be used in Page (which can be further used in a master page)

To form the grid, we need some information from the page which is passed in Init Event of page and which will be available at max in the load event of user control.

So, we cannot restrict making the user control in Page Init in User Control.

Please suggest a way to do the same.

Please mark a copy of solution to:
chetan.mehta@cognizant.com

Regards
Chetan
Frameworks team, marketRx (A Cognizant company)

P.S: We have purchased Telerik UI suite but raising ticket here since the subscription has expired.


0
Nikolay Rusev
Telerik team
answered on 21 Apr 2009, 06:48 AM
Hello Cmehta,

Attached to this post you will find sample how you can create RadGrid programmatically and use GridTemplateColumn.
Please give it a try and see whether it helps.

Kind regards,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Timothy straub
Top achievements
Rank 1
answered on 09 Oct 2009, 06:53 PM
Hi,

I am dynamically creating a grid in a usercontrol using the same example of radgridtemplates.zip.
Instead of the Label template column , I am using TextBox template column.

Now when the user clicks on Save button on the page, the updated grid data is lost between page post back.

How can I persist the grid data.

Thanks,
Saifee.A
0
Nikolay Rusev
Telerik team
answered on 12 Oct 2009, 01:15 PM
Hello Timothy,

The demo illustrates how to build ItemTemplate which is render in RadGrid view mode.
I guess you are creating EditItemTemplate and use it for RadGrid edit mode. In this case you must implement IBindableTemplate instead.

Greetings,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
cmehta
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
cmehta
Top achievements
Rank 1
Timothy straub
Top achievements
Rank 1
Share this question
or