ContextMenu does not give current item or row index with sub grids

I have a form with sub grids, and I was trying to apply a context menu to them. The context menu works fine when used from the parent grid, but when used from the children grids there is no way to retrieve the current item or row that I can see. I have attached an example. In the first two events that are bolded, the indexes always return as -1.


        private void transformsGrid_DeleteRowClicked(object sender, EventArgs e)
        {
            Debug.WriteLine(nameof(transformsGrid_DeleteRowClicked));
            Debug.WriteLine($"SelectedIndex: {_transformsGrid.SelectedIndex}, CurrentCell.RowIndex: {_transformsGrid.CurrentCell.RowIndex}");
        }


        private void columnsGrid_DeleteRowClicked(object sender, EventArgs e)
        {
            Debug.WriteLine(nameof(columnsGrid_DeleteRowClicked));
            Debug.WriteLine($"SelectedIndex: {_columnsGrid.SelectedIndex}, CurrentCell.RowIndex: {_columnsGrid.CurrentCell.RowIndex}");
        }


        private void tablesGrid_DeleteRowClicked(object sender, EventArgs e)
        {
            Debug.WriteLine(nameof(tablesGrid_DeleteRowClicked));
            Debug.WriteLine($"SelectedIndex: {tablesGrid.SelectedIndex}, CurrentCell.RowIndex: {tablesGrid.CurrentCell.RowIndex}");
        }

Attachment: Form1_17d0010f.zip

5 Replies

DM Dhanasekar Mohanraj Syncfusion Team June 30, 2022 03:28 PM UTC

HI Tim,

You can retrieve the selected index of the detailsview daatgrid using below code snippet,

C#:

private void transformsGrid_DeleteRowClicked(object sender, EventArgs e)

{

    Debug.WriteLine(nameof(transformsGrid_DeleteRowClicked));

    Debug.WriteLine($"SelectedIndex: {(tablesGrid.SelectedDetailsViewGrid as SfDataGrid).SelectedIndex}, CurrentCell.RowIndex: {(tablesGrid.SelectedDetailsViewGrid as SfDataGrid).CurrentCell.RowIndex}");

}

 

private void columnsGrid_DeleteRowClicked(object sender, EventArgs e)

{

    Debug.WriteLine(nameof(columnsGrid_DeleteRowClicked));

    Debug.WriteLine($"SelectedIndex: {(tablesGrid.SelectedDetailsViewGrid as SfDataGrid).SelectedIndex}, CurrentCell.RowIndex: {(tablesGrid.SelectedDetailsViewGrid as SfDataGrid).CurrentCell.RowIndex}");

}

 

private void tablesGrid_DeleteRowClicked(object sender, EventArgs e)

{

    Debug.WriteLine(nameof(tablesGrid_DeleteRowClicked));

    Debug.WriteLine($"SelectedIndex: {tablesGrid.SelectedIndex}, CurrentCell.RowIndex: {tablesGrid.CurrentCell.RowIndex}");

}


We have prepared the sample for the same. Please have a look on this and revert us if you need further assistance for this.

Regards,
Dhanasekar M.


Attachment: SfDataGridDemo_b93d1a1d.zip


TI Tim October 26, 2022 07:37 PM UTC

So, I FINALLY got back around to looking at this, and your solution is giving the wrong information. It applies to the parent grid, and NEVER EVER changes no matter what row / cell right click in a sub grid.



TI Tim October 26, 2022 08:05 PM UTC

Ahhhh I see now... you need to go down as many levels as there are subgrids. So for the example, to get to the transform grid you need to do like so:


((tablesGrid.SelectedDetailsViewGrid as SfDataGrid).SelectedDetailsViewGrid as SfDataGrid).CurrentCell





TI Tim October 26, 2022 08:28 PM UTC

Here is a modified version of the example code that gets the correct output:

        private void transformsGrid_DeleteRowClicked(object sender, EventArgs e)

        {

            Debug.WriteLine(nameof(transformsGrid_DeleteRowClicked));

            var grid = ((tablesGrid.SelectedDetailsViewGrid as SfDataGrid).SelectedDetailsViewGrid as SfDataGrid);

            Debug.WriteLine($"SelectedIndex: {grid.SelectedIndex}, CurrentCell.ColumnIndex: {grid.CurrentCell.ColumnIndex}, CurrentCell.RowIndex: {grid.CurrentCell.RowIndex}");

        }


        private void columnsGrid_DeleteRowClicked(object sender, EventArgs e)

        {

            Debug.WriteLine(nameof(columnsGrid_DeleteRowClicked));

            var grid = (tablesGrid.SelectedDetailsViewGrid as SfDataGrid);

            Debug.WriteLine($"SelectedIndex: {grid.SelectedIndex}, CurrentCell.ColumnIndex: {grid.CurrentCell.ColumnIndex}, CurrentCell.RowIndex: {grid.CurrentCell.RowIndex}");

        }


        private void tablesGrid_DeleteRowClicked(object sender, EventArgs e)

        {

            Debug.WriteLine(nameof(tablesGrid_DeleteRowClicked));

            Debug.WriteLine($"SelectedIndex: {tablesGrid.SelectedIndex}, CurrentCell.RowIndex: {tablesGrid.CurrentCell.RowIndex}");

        }





DM Dhanasekar Mohanraj Syncfusion Team October 27, 2022 02:04 PM UTC

Hi Tim,


We are glad that the reported issue was resolved on your side. Please let us know if you need any other details on this. As always, we will be happy to assist you.


Regards,

Dhanasekar M.


Loader.
Up arrow icon