Setting ReadOnly property on AutoGenerated columns

4 posts, 0 answers
  1. Greg S
    Greg S avatar
    8 posts
    Member since:
    Jul 2015

    Posted 19 Aug 2015 Link to this post

    I'm creating a very generic table editor and would like to set the readonly property of a grid column at run time. The select and update SQL commands are being generated at runtime and they work perfectly with AutoGenerateColumns="true", but I need to set some columns to readonly. I was hoping to simply enumerate the columns after data binding or on the grid's pre_render, but the column count is always 0 for auto generated columns. 

    I looked at grid.MasterTableView.RenderColumns, but each column's IsEditable property is read-only.

    Anyone know how to accomplish this? I'm trying to avoid dynamically creating each column in code and then appending them to the collection.

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2252 posts

    Posted 21 Aug 2015 Link to this post

    Hello Greg,

    In order to implement the behavior you can handle the ColumnCreated event. It is fired for every autogenerated column. In the handler you can check what is the currently created column and set its ReadOnly property accordingly.

    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if ((e.Column is GridBoundColumn))
        {
            GridBoundColumn boundColumn = e.Column as GridBoundColumn;
     
            if (boundColumn.DataField == "ID")
            {
                boundColumn.ReadOnly = true;
            }
          
        }
    }



    Regards,
    Viktor Tachev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Greg S
    Greg S avatar
    8 posts
    Member since:
    Jul 2015

    Posted 21 Aug 2015 in reply to Viktor Tachev Link to this post

    Thanks, Viktor! Just what I needed.  Now here's a follow up... if I wanted to convert an auto-generated column's edit control from a text box to a drop down, how would I do that? I have some columns that are foreign keys to other tables.
  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2252 posts

    Posted 24 Aug 2015 Link to this post

    Hello Greg,

    In order to change the default editor for a column you can define a custom editor control. You can add that editor to the column in the CreateColumnEditor event handler. Check out the following article that illustrates the approach.


    With that said, it would be easier to define the columns in RadGrid declaratively in the markup. This way you would be able to use GridDropDownColumn that renders a dropdown control in edit mode out of the box. If you would like additional information on the GridDropDownColumn you would find the following article interesting.



    Regards,
    Viktor Tachev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top