How To Disable Edit Of A Particular RadGrid Cell And Hide The Edit Control



TODO:

Have you ever wanted to remove a TextBox or other Control from a particular cell in a RadGrid?  This is particularly useful when you want to conditionally allow edits based on certain data in the record.

 

SOLUTION:

protected void rgMyGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
        //do it when in edit mode
     if (e.Item.IsInEditMode && e.Item is GridEditableItem)
     {
          GridEditableItem dataItem = e.Item as GridEditableItem;

          //if we have a particular type, do not allow the cell to be edited.  if it is not this type, we can allow the cell to be edited
          if (((Employee)dataItem.DataItem).SSN == Ssn1 ||
                ((Employee)dataItem.DataItem).SSN == Ssn2)
          {
                //we have one that needs to have name edit disabled and hidden
                dataItem["Name"].Enabled = false;
                dataItem["Name"].Controls[0].Visible = false;

                //now create a label and add it to the controls of the "Name" cell.  Set the text of the label to the text that is in the edit TextBox that we hid.
                Label descLabel = new Label();
                descLabel.Text = (dataItem["Name"].Controls[0] as TextBox).Text;
                dataItem["Name"].Controls.Add(descLabel);
          }
     }
}

 

NOTES:

There are no notes on this topic