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

Conditional Readonly Columns

2 Answers 150 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steele
Top achievements
Rank 1
Steele asked on 29 Apr 2008, 06:41 PM
I need to make some columns ReadOnly conditionally in Item_DataBound
How to I reference the ReadOnly attributeof the column properly here?
I can't do it in Item_Created because I need the bound DataItem.

The following code is setting the wrong item...
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then  
            'switch the Item to Allow Edits of ListName if it is UserDefined  
            If DirectCast(e.Item.DataItem, InvoiceItem).ItemTypeID = "U" Then  
                'switching wrong row?  
                DirectCast(e.Item.OwnerTableView.GetColumnSafe("ListName"), GridBoundColumn).ReadOnly = False 
                DirectCast(e.Item.OwnerTableView.GetColumnSafe("Amount"), GridBoundColumn).ReadOnly = False 
            End If  
 

The above code sets only the first item in the grid, not the item I am referencing.
I can't seem to access the ReadOnly attribute with anything else.

2 Answers, 1 is accepted

Sort by
0
Steele
Top achievements
Rank 1
answered on 29 Apr 2008, 06:57 PM
This works in Item_Databound, but I don't like it because it uses Ordinals...
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then 
            'switch the Item to Allow Edits of ListName and Amount if it is UserDefined  
            If DirectCast(e.Item.DataItem, InvoiceItem).ItemTypeID = "U" Then 
                Dim item = DirectCast(e.Item, GridEditableItem)  
                DirectCast(item("ListName").Controls(0), TextBox).Visible = True 
                DirectCast(item("ListName").Controls(1), Literal).Visible = False 
                DirectCast(item("Amount").Controls(0), TextBox).Visible = True 
                DirectCast(item("Amount").Controls(1), Literal).Visible = False 
            End If 
 

If there is some better way, please let me know.

0
Shinu
Top achievements
Rank 2
answered on 30 Apr 2008, 05:32 AM
Hi,

Try setting the ReadOnly property for the GridColumn in the PreRender event as shown below.

CS:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridColumn column in RadGrid1.MasterTableView.RenderColumns) 
        { 
            if (column.ColumnType == "GridBoundColumn") 
            { 
                GridBoundColumn col = (GridBoundColumn)column; 
                if (col.UniqueName == "Name") 
                { 
                    col.ReadOnly = true
                } 
            } 
        } 
    } 


Thanks
Shinu.
Tags
Grid
Asked by
Steele
Top achievements
Rank 1
Answers by
Steele
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or