Using same button cell click event for multiple grid controls

Hello,

I have multiple grids in a form that all have button cells. The click event points to the same method for all grid controls. How can I find out which grid the cell belongs to in the event? Thanks ahead.



3 Replies

VS Vijayarasan Sivanandham Syncfusion Team July 6, 2021 03:30 PM UTC

Hi Andreas Bartels,

Thank you for contacting Syncfusion Support.

Your requirement can be achieved by define the name for DataGrid and Get the DataGrid in CellButtonClick event by using Reflection. Please refer the below code snippet, 
//First DataGrid 
sfDataGrid.AutoGenerateColumns = false; 
sfDataGrid.DataSource = new ViewModel().Orders;            
sfDataGrid.Name = "FirstDataGrid"; 
sfDataGrid.CellButtonClick += SfDataGrid_CellButtonClick; 
 
//Second DataGrid 
var table = this.GetDataTable(); 
sfDataGrid1.Name = "SecondDataGrid";             
sfDataGrid1.DataSource = table;             
sfDataGrid1.CellButtonClick += SfDataGrid_CellButtonClick; 
 
private void SfDataGrid_CellButtonClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellButtonClickEventArgs e) 
{ 
            //get the cellbutton clicke DataGrid   
            var dataGrid = e.Record.GetType().GetProperty("DataGrid", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(e.Record) as SfDataGrid; 
             
            //check the datagrid based on SfDataGrid Name property 
            if(dataGrid.Name == "FirstDataGrid") 
            { 
                MessageBox.Show(dataGrid.Name); 
            } 
            else if(dataGrid.Name == "SecondDataGrid") 
            { 
                MessageBox.Show(dataGrid.Name); 
            } 
} 
Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 



AB Andreas Bartels July 7, 2021 04:08 PM UTC

Works like a charm, thanks a lot!




VS Vijayarasan Sivanandham Syncfusion Team July 8, 2021 05:33 AM UTC

Hi Andreas Bartels,

Thanks for the update.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S


Loader.
Up arrow icon