Only allow selection within the same column

Hi,

I am using the SfDataGrid and I would like to know if it is possible to only allow for cells within the same column to be selected?

The user are allowed to click on all cells in the grid, but as soon the user begins to select multible cells, whether it is with CTRL or dragging the mouse, then it should ignore cell selection on cells from another column than the first selected cell.

Is this possible?


4 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team July 7, 2023 11:20 AM UTC

Hi Morten Jørgensen,

Your requirement to “Make the first clicked column only selectable and restrict other columns from the selection” will be achievable by using the SfDataGrid.SelectionChanging event as shown below,

this.sfDataGrid1.SelectionChanging += OnSfDataGrid1SelectionChanging;

private
void OnSfDataGrid1SelectionChanging(object sender, SelectionChangingEventArgs e)

{

    if (firstClickedColumnIndex == -1)

    {

        // Set the index of the first clicked column

        var column = (e.AddedItems[0] as SelectedCellInfo).Column;

        firstClickedColumnIndex = this.sfDataGrid1.Columns.IndexOf(column);

        firstClickedColumnIndex = this.sfDataGrid1.TableControl.ResolveToScrollColumnIndex(firstClickedColumnIndex);

    }

    else

    {

        // Disable selection for other columns

        foreach (var item in e.AddedItems)

        {

            var otherColumn = (item as SelectedCellInfo).Column;

            int columnIndex = this.sfDataGrid1.Columns.IndexOf(otherColumn);

            columnIndex = this.sfDataGrid1.TableControl.ResolveToScrollColumnIndex(columnIndex);

            if (columnIndex != firstClickedColumnIndex)

            {

                e.Cancel = true;

                break;

            }

        }

    }

      

}


Here, we have prepared the sample based on your scenario, please have a look at this.

Regards,

Dhanasekar M.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDatagridDemo_93d41537.zip

Marked as answer

MJ Morten Jørgensen July 7, 2023 12:48 PM UTC

Hi  Dhanasekar

This works great but not exactly what I was looking for. It is a little bit hard to explain :)

In your example, if I click a cell in column 5 one time, then I can only select another cell in the same column even if I click in another cell from a different column.

I want to allow the user to click in a cell in column 6 and then start a selection there, but not simulations with the first selection. Lets say I select 5 cells in column 5 and select/click a cell in column 6, then the 5 cells should be unselected and a new selection starts in column 6.

So when I start to select cells, it should only be within in same column, until I want to make another selection in another column.


Hope this helps.


Regards Morten



MJ Morten Jørgensen July 7, 2023 12:53 PM UTC

Hi again


I think I just solved it myself by adding this:

private void sfDataGrid1_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
{
firstClickedColumnIndex = -1;
}

This resets the firstClickedColumnIndex every time I click a cell.


Do you have any comments on this way of doing it?


Regards Morten



DM Dhanasekar Mohanraj Syncfusion Team July 10, 2023 11:45 AM UTC

Morten Jørgensen,

We have reviewed your requirement and the provided code snippet. It appears that you are correctly resetting the firstClickedColumnIndex variable using the SfDataGrid.CellClick event. This approach allows you to select multiple cells in the same column, as per your requirement.


Loader.
Up arrow icon