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

radgridview, how to lock a row(can not select other row, but can edit this row)

3 Answers 236 Views
GridView
This is a migrated thread and some comments may be shown as answers.
deng bruce
Top achievements
Rank 1
deng bruce asked on 07 Sep 2011, 08:28 AM
thanks

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 08 Sep 2011, 08:10 AM
Hello deng bruce,

I will propose the following solution:
public partial class Form1 : Form
{
    private RadGridView radGridView1 = GridViewHelper.CreateGridView();
    private Locker<GridViewDataRowInfo> rowLocker;
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1);
        radGridView1.DataSource = DataSources.GetListDataSource();
 
        var btnLock = new RadButton();
        btnLock.Text = "Lock";
        this.Controls.Add(btnLock);
        btnLock.Dock = DockStyle.Bottom;
        btnLock.Click += new EventHandler(btnLock_Click);
 
        var btnUnlock = new RadButton();
        btnUnlock.Text = "Unlock";
        this.Controls.Add(btnUnlock);
        btnUnlock.Dock = DockStyle.Bottom;
        btnUnlock.Click += new EventHandler(btnUnlock_Click);
 
        radGridView1.CurrentRowChanging += new CurrentRowChangingEventHandler(radGridView1_CurrentRowChanging);
    }
 
    void radGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
    {
        if (rowLocker != null && rowLocker.IsLocked)
        {
            e.Cancel = true;
        }
    }
 
    void btnUnlock_Click(object sender, EventArgs e)
    {
        if (rowLocker != null)
        {
            rowLocker.Dispose();
            //or
            rowLocker.UnLock();
        }
    }
 
    void btnLock_Click(object sender, EventArgs e)
    {
        if (radGridView1.CurrentRow == null)
        {
            return;
        }
 
        var dataRow = radGridView1.CurrentRow as GridViewDataRowInfo;
        if (dataRow != null)
        {
            if (rowLocker == null)
            {
                rowLocker = new Locker<GridViewDataRowInfo>();
            }
 
            rowLocker.Lock((GridViewDataRowInfo)radGridView1.CurrentRow);
        }
    }
 
    private class Locker<T> : IDisposable where T : class
    {
        public bool IsLocked { get; private set; }
 
        public T LockedObject { get; set; }
 
        public Locker()
        {
 
        }
 
        public Locker(bool isLocked, T lockedObject)
        {
            IsLocked = isLocked;
            LockedObject = lockedObject;
        }
 
        public void Lock(T lockedObject)
        {
            IsLocked = true;
            LockedObject = lockedObject;
        }
 
        public void UnLock()
        {
            IsLocked = false;
            LockedObject = null;
        }
 
        public void Dispose()
        {
            UnLock();
        }
    }
}


If you need still need any any help on this one, please let me know.

btw: there are some helper methods missing here, if you need them please just say so :)

Best Regards,
Emanuel Varga
0
deng bruce
Top achievements
Rank 1
answered on 08 Sep 2011, 08:20 AM
thanks a lot.
0
Ivan Petrov
Telerik team
answered on 08 Sep 2011, 03:01 PM
Hi Deng Bruce,

Thank you for writing.

You can use Emanuel's solution which is the more elegant way to go about this or you can simply always cancel the CurrentRowChanging event and when changing the row, to unsubscribe, change the row and the subscribe again. See the code snippet below:

private void radGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
{
  e.Cancel = true;
}

And when selecting different row:

this.radGridView1.CurrentRowChanging -= radGridView1_CurrentRowChanging;
this.radGridView1.Rows[index].IsCurrent = true;
this.radGridView1.CurrentRowChanging += radGridView1_CurrentRowChanging;

I hope this helps. 

Best wishes,
Ivan Petrov
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
deng bruce
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
deng bruce
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or