How to Get a Cell's Grid Name?

In my OnCellClick method, how can I get the name of the cell's datagrid. Presumably this can be derived from the event arguments (e), but I can't figure out how to get there.

I have multiple grids using the same onclick actions and I want to put the actions into a single method (with if/else logic) rather than creating a separate method for each grid.

I assume whatever solution you recommend will also work in my QueryRowStyle and QueryCellStyle events?

I am using C#.


Thank you.


5 Replies

MA Mohanram Anbukkarasu Syncfusion Team July 15, 2021 09:42 AM UTC

Hi tradecode, 

Thanks for contacting Syncfusion products.  

You can get the name of the corresponding SfDataGrid in the CellClick event by using the sender argument as shown in the following code example.  

Code example :  

private void SfDataGrid1_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e) 
{ 
    if((sender as SfDataGrid).Name =="sfDataGrid1") 
    { 
 
    } 
} 

You can use the same for QueryRowStyle and QueryCellStyle events. Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 



TR tradecode July 15, 2021 01:28 PM UTC

This works well. Thank you very much!





MA Mohanram Anbukkarasu Syncfusion Team July 16, 2021 08:42 AM UTC

Hi tradecode, 

Thanks for the update.  

We are glad to know that the provided solution worked at your end. Please let us know if you require any other assistance from us. We are happy to help you.  

Regards, 
Mohanram A.  



AT Alison Tan October 26, 2022 08:17 AM UTC

Is there a way to let the same approach work the "CellButtonClickEventArgs" event as well?

For instance:

private void SfDataGrid1_CellButtonClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellButtonClickEventArgs e) 
{ 
    if((sender as SfDataGrid).Name =="sfDataGrid1") 
    { 
    } 
} 


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

Hi Alison Tan,

You can achieve your requirement to get the SfDataGrid’s Name by using the CellButtonClickEventArgs shown below,

this.sfDataGrid1.CellButtonClick += OnCellButtonClick;

private void OnCellButtonClick(object sender, CellButtonClickEventArgs e)

{

    SfDataGrid datagrid = ((e.Record as Syncfusion.WinForms.DataGrid.DataRow).GetType().GetProperty("DataGrid", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(e.Record as Syncfusion.WinForms.DataGrid.DataRow) as SfDataGrid);

    if (datagrid.Name == "sfDataGrid1")

    {

 

    }

}


We have prepared the sample based on your scenario. Please have a look at this and revert us if you need further assistance with 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_6a643e7e.zip

Loader.
Up arrow icon