RadGrid for ASP.NET AJAX

RadControls Send comments on this topic.
Setting edit permissions depending on permission field
Controls > RadGrid > Insert/Update/Delete records > How-to > Setting edit permissions depending on permission field

Glossary Item Box

When a user log in you may want to set permissions for editing grid items depending on the security settings for the corresponding account. For this purpose you can create an extra boolean column in the grid data source which reflects the permission status for the logged user (we can call it permission column). Then in the ItemDataBound event of Telerik RadGrid you can traverse all GridDataItems, check the value of the permission column (true or false) and enable/disable the [Edit] button for the currently bound grid item to allow/prevent the edit operation for it.

In the example below we assume that the Bool column in the grid data source is our permission column. Note that you can hide the column if you wish by setting its Display property to false:


ASPX/ASCX Copy Code
<telerik:RadGrid id="RadGrid1" runat="server">
   
<MasterTableView AutoGenerateColumns="False">
    
<Columns>
     
<telerik:GridBoundColumn HeaderText="Contact name" DataField="ContactName" UniqueName="Contact name"></telerik:GridBoundColumn>
     
<telerik:GridBoundColumn HeaderText="Contact title" DataField="ContactTitle" UniqueName="ContactTitle"></telerik:GridBoundColumn>
     
<telerik:GridBoundColumn HeaderText="City" DataField="City" UniqueName="City"></telerik:GridBoundColumn>
     
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
     
<telerik:GridCheckBoxColumn DataField="Bool" ReadOnly="True" UniqueName="CheckBoxColumn" Display="True"></telerik:GridCheckBoxColumn>
    
</Columns>
   
</MasterTableView>
</
telerik:RadGrid>

And in the code-behind:

C# Copy Code
 private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
 {
  
if(e.Item is GridDataItem)
  {
   GridDataItem dataItem = e.Item
as GridDataItem;
   
if(!(dataItem["CheckBoxColumn"].Controls[0] as CheckBox).Checked)
   {
       (dataItem[
"EditCommandColumn"].Controls[0] as LinkButton).Enabled = false;
   }
  }
 }
VB.NET Copy Code
Private Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)
   If TypeOf e.Item Is GridDataItem Then
      Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
      If Not CType(dataItem("CheckBoxColumn").Controls(0), CheckBox).Checked Then
          CType(dataItem( "EditCommandColumn").Controls(0), LinkButton).Enabled = False
      End If
   End If
End Sub 'RadGrid1_ItemDataBound