)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to protect or unprotect a selected range of formula cells in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 18, 2019
Tags: formula, readonly

Protect or Unprotect the selected range of formula cells

In the GridControl, the selected ranges of formula cells are protected with the help of the ReadOnly Property. To protect a selected range of cells by using Mouse or Keyboard, Click Tools->Protection-> Protect Cells. To unprotect a selected range of cells by using Mouse or Keyboard, Click Tools->Protection->Unprotect Cells.

C#

GridStyleInfo headerStyle = new GridStyleInfo();
private void menuItem7_Click(object sender, System.EventArgs e)
{
    this.gridControl1.IgnoreReadOnly=false;
    //To protect selected Formula Cells.
    this.gridControl1.CurrentCell.MoveTo(-1,-1);
    headerStyle.ReadOnly =true;
    headerStyle.BackColor=Color.LightPink;
    foreach(GridRangeInfo r in this.gridControl1.Selections.Ranges)
    {
        this.gridControl1.ChangeCells(r,   headerStyle,Syncfusion.Styles.StyleModifyType.Override);
    }
    this.gridControl1.Selections.Clear();
}
private void menuItem8_Click(object sender, System.EventArgs e)
{
    //To unprotect Selected Formula Cells.
    this.gridControl1.CurrentCell.MoveTo(-1,-1);
    this.gridControl1.IgnoreReadOnly=true;
    headerStyle.ReadOnly = false;
    headerStyle.BackColor=Color.White; //.FromKnownColor(System.Drawing.KnownColor.White);
    foreach(GridRangeInfo r in this.gridControl1.Selections.Ranges)
    {
        this.gridControl1.ChangeCells(r,     headerStyle,Syncfusion.Styles.StyleModifyType.Override);
    }
    this.gridControl1.IgnoreReadOnly=false;
    this.gridControl1.Selections.Clear();
}

VB

Private headerStyle As New GridStyleInfo()
Private Sub menuItem7_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Me.gridControl1.IgnoreReadOnly=False
    'To protect selected Formula Cells.
    Me.gridControl1.CurrentCell.MoveTo(-1,-1)
    headerStyle.ReadOnly =True
    headerStyle.BackColor=Color.LightPink
    For Each r As GridRangeInfo In Me.gridControl1.Selections.Ranges
        Me.gridControl1.ChangeCells(r, headerStyle,Syncfusion.Styles.StyleModifyType.Override)
    Next r
    Me.gridControl1.Selections.Clear()
End Sub
Private Sub menuItem8_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    'To unprotect Selected Formula Cells.
    Me.gridControl1.CurrentCell.MoveTo(-1,-1)
    Me.gridControl1.IgnoreReadOnly=True
    headerStyle.ReadOnly = False
    headerStyle.BackColor=Color.White '.FromKnownColor(System.Drawing.KnownColor.White);
    For Each r As GridRangeInfo In Me.gridControl1.Selections.Ranges
        Me.gridControl1.ChangeCells(r, headerStyle,Syncfusion.Styles.StyleModifyType.Override)
    Next r
    Me.gridControl1.IgnoreReadOnly=False
    Me.gridControl1.Selections.Clear()
End Sub

After applying the properties, the grid looks as follows.

Protect the selected range of formula cell

Figure 1: Protect the selected range of formula cell

Samples:

C#: Protect/Unprotect Cells

VB: Protect/Unprotect Cells

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon