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

Preventing a row from being selected

3 Answers 671 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 03 Feb 2012, 10:13 PM
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

3 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 04 Feb 2012, 07:29 PM
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 >>
1
Mark
Top achievements
Rank 1
answered on 06 Feb 2012, 05:23 PM
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.
1
Shreya
Top achievements
Rank 1
answered on 29 Apr 2015, 01:03 PM

Hi Mark,

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

Pradeep
Top achievements
Rank 1
commented on 03 Dec 2021, 09:59 AM

Someone please explain how to handle the "Select All" checkbox for this scenario?

On checking the Select All checkbox, only the enabled checkboxes of the grid items should get checked. Disabled ones should remain unchecked. 

Please help..

Martin Ivanov
Telerik team
commented on 06 Dec 2021, 01:32 PM

I've prepared a small example showing how to achieve this. The sample uses the IsReadOnlyBinding property of the column, instead of a custom column. I hope that helps.
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Mark
Top achievements
Rank 1
Shreya
Top achievements
Rank 1
Share this question
or