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

Setting ReadOnly property on AutoGenerated columns

3 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Greg S
Top achievements
Rank 1
Greg S asked on 19 Aug 2015, 07:03 PM

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.

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 21 Aug 2015, 12:46 PM
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
0
Greg S
Top achievements
Rank 1
answered on 21 Aug 2015, 03:55 PM
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.
0
Viktor Tachev
Telerik team
answered on 24 Aug 2015, 12:24 PM
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
Tags
Grid
Asked by
Greg S
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Greg S
Top achievements
Rank 1
Share this question
or