Column ReadOnly true at EditMode but false at InsertMode

4 posts, 1 answers
  1. Naunton
    Naunton avatar
    70 posts
    Member since:
    Jun 2010

    Posted 18 Jan 2011 Link to this post

    Hi all,

    I want to set a column to ReadOnly=True at EditMode, but at InsertMode ReadOnly=False. Please advise how to do it.
    I did as below but it didn't work. Please help.

    Private Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
    If e.CommandName = RadGrid.EditCommandName Then
                Dim editItem As GridEditableItem = CType(RadGrid1.MasterTableView.GetItems(GridItemType.EditItem)(0), GridEditableItem)
                For Each column As GridColumn In RadGrid1.MasterTableView.Columns
                    If column.UniqueName = "POL" Then
                        Dim txtbox As TextBox = CType(editItem (column.UniqueName).Controls(0), TextBox)
                        txtbox.ReadOnly = True
                    End If
                Next
    End If
    End Sub

    But I got error  "Index was outside the bounds of the array." at the below code ..
    If e.CommandName = RadGrid.EditCommandName Then
                Dim editItem As GridEditableItem = CType(RadGrid1.MasterTableView.GetItems(GridItemType.EditItem)(0), GridEditableItem)

    Many Thanks
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jan 2011 Link to this post

    Hello Naunt,

    You can set the ReadOnly property for control in edit/insert mode in ItemdataBound event.

    VB.NET:
    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If e.Item.OwnerTableView.IsItemInserted Then
            'item is about to be inserted  and EditMode is 'InPlace'
            If TypeOf e.Item Is GridDataInsertItem Then
                Dim inserItem As GridDataInsertItem = DirectCast(e.Item, GridDataInsertItem)
                Dim txtbox As TextBox = DirectCast(inserItem("POL").Controls(0), TextBox)
                txtbox.[ReadOnly] = False
            End If
        End If
        If Not (TypeOf e.Item Is GridDataInsertItem) AndAlso e.Item.IsInEditMode Then
            'item is about to be edit and EditMode is 'InPlace'
            Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim txtbox As TextBox = DirectCast(editItem("POL").Controls(0), TextBox)
            txtbox.[ReadOnly] = True
        End If
    End Sub

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Naunton
    Naunton avatar
    70 posts
    Member since:
    Jun 2010

    Posted 18 Jan 2011 Link to this post

    Hi Princy,

    Many thanks, this work very well.  :)
  5. umesh
    umesh avatar
    1 posts
    Member since:
    Apr 2015

    Posted 22 Apr 2015 Link to this post

    How can I write that in c#?

     

    thanks

Back to Top