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

4 posts, 0 answers
  1. deng bruce
    deng bruce avatar
    5 posts
    Member since:
    Mar 2010

    Posted 07 Sep 2011 Link to this post

    thanks
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 08 Sep 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. deng bruce
    deng bruce avatar
    5 posts
    Member since:
    Mar 2010

    Posted 08 Sep 2011 Link to this post

    thanks a lot.
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 08 Sep 2011 Link to this post

    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 >>

Back to Top