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

How to restrict the new item template on a dynamically added column

3 Answers 60 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 16 Jan 2012, 02:48 PM
Hi there,  I'm adding a dynamic column at run-time, but when the user clicks the "new record" button, I don't want the new column to have the ability of having user input.

I know that if I was doing this at design-time, then I would just add empty  <EditItemTemplate></EditItemTemplate> tags.

So, at the moment - I'm building my column like this :

   'Add a new column showing the data from the attribute search box
        Dim objBoundColumn As New GridBoundColumn
        radgridContacts.MasterTableView.Columns.Add(objBoundColumn)         objBoundColumn.DataField = AttributeList.SelectedItem.Value         objBoundColumn.HeaderText = AttributeList.SelectedItem.Value         objBoundColumn.UniqueName = "tempColumn"

What I'd like to do is add something like :
objBoundColumn.EditItemTemplate = False 

Can anyone out there tell me how to do that please ?


3 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 17 Jan 2012, 10:32 AM
Hi again,
This seems like a lot of code, and a pain in the arse way of getting round things - but I've got round the problem.

So - it looks like I can't get round the issue with the GridBoundColumn...
 (@Telerik developers - maybe you should consider building this functionality into the next release !!  objGridBoundColumn.EditItemTemplate = False )
 
So I've had to use a GrifdTemplateColumn, and here's my solution:

 'Add a new column showing the data from the attribute search
                    Dim templateColumn As New GridTemplateColumn
                    templateColumn.DataField = ddlUserAttributes.SelectedItem.Value
                    templateColumn.HeaderText = ddlUserAttributes.SelectedItem.Value                     templateColumn.ItemTemplate = New MyTemplate(ddlUserAttributes.SelectedItem.Value)                     templateColumn.UniqueName = "tempColumn"                     radgridContacts.MasterTableView.Columns.Add(templateColumn)
		radgridContacts.DataSource = objContacts
                radgridContacts.DataBind()

and then I added this Friend class to the bottom of the code behind page :

Friend Class MyTemplate
    Implements ITemplate
    Protected litControl As LiteralControl
    Private colname As String
 
    Public Sub New(ByVal cName As String)
        colname = cName
    End Sub
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
        litControl = New LiteralControl()
        litControl.ID = "lControl"
        AddHandler litControl.DataBinding, AddressOf litControl_DataBinding
 
        container.Controls.Add(litControl)
 
        litControl_DataBinding(litControl, Nothing)
 
    End Sub
 
    Public Sub litControl_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
        Dim l As LiteralControl = DirectCast(sender, LiteralControl)
        Dim container As GridDataItem = DirectCast(l.NamingContainer, GridDataItem)
 
        Try
            Dim objContact As New CommuniGator.Contact
            objContact = DirectCast(container.DataItem, CommuniGator.Contact)
 
            l.Text = objContact.GetType().GetProperty(colname).GetValue(objContact, Nothing)
        Catch ex As Exception
            'Probably a mix up of user searching an attribute, but also trying to add a new record
        End Try
 
    End Sub
End Class

0
Tsvetina
Telerik team
answered on 18 Jan 2012, 10:19 AM
Hello Scott,

Why not use the ReadOnly property of the column, this way it will not display an edit control when the grid is in edit mode and you do not need to add any custom code to prevent it from allowing editing.

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
0
Scott
Top achievements
Rank 1
answered on 19 Jan 2012, 04:37 PM
Damn it !
That was MUCH easier !

Cheers
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or