Preventing a row from being selected

4 posts, 0 answers
  1. Mark
    Mark avatar
    44 posts
    Member since:
    Jun 2009

    Posted 03 Feb 2012 Link to this post

    Hello,

    I have a RadGridView that uses a GridViewSelectColumn to facilitate row selection.  However, under certain circumstances, I would like to prevent certain rows from being selected.  Is there any way that I can prevent rows from being selectable?

    Thanks,
    Mark
  2. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 04 Feb 2012 Link to this post

    Hi Mark,

     

    You may subscribe to the SelectionChanging event of RadGridView and track the CanSelect property from  there. Just cancel the event if the row should not be selected.


    Greetings,
    Vanya Pavlova
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Mark
    Mark avatar
    44 posts
    Member since:
    Jun 2009

    Posted 06 Feb 2012 Link to this post

    Hi Vanya,

    Thanks for your reply.  Yes, that is one solution, but it would still present an enabled checkbox to the user.  I was looking for a way to disable the checkbox in the GridViewSelectColumn.  So, the solutionI went with was pretty simple in the end - I created a custom column as follows.  (Hopefully it might help someone else out there.)

    public class CounterSelectionColumn
        : GridViewColumn
    {
        public CounterSelectionColumn()
        {
            IsResizable = false;
            IsReorderable = false;
            Width = GridViewLength.Auto;
            MinWidth = 25;
        }
        public override bool CanFilter()
        {
            return false;
        }
        public override bool CanSort()
        {
            return false;
        }
        public override bool CanGroup()
        {
            return false;
        }
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            if (cell != null)
            {
                CheckBox checkBox = cell.Content as CheckBox 
                    ?? new CheckBox
                    {
                        IsTabStop = false,
                    };
                checkBox.SetBinding(UIElement.IsEnabledProperty,
                    new Binding("IsAvailable")
                    {
                        Source = dataItem,
                        Mode = BindingMode.OneWay
                    });
                var row = cell.ParentRow;
                if (row != null)
                {
                    checkBox.SetBinding(ToggleButton.IsCheckedProperty,
                        new Binding("IsSelected")
                        {
                            Source = row,
                            Mode = BindingMode.TwoWay
                        });
                }
                return checkBox;
            }
            return base.CreateCellElement(cell, dataItem);
        }
    }

    Thanks,
    Mark.
  4. Shreya
    Shreya avatar
    18 posts
    Member since:
    Aug 2014

    Posted 29 Apr 2015 in reply to Mark Link to this post

    Hi Mark,

    Thanks a lot your solution worked. Can you tell me how to have a Select all checkbox for this?

Back to Top