getting error when click the datagridview header

Gents,

I am using this sfdatagrid and my intention is to get the value using 

private void dataGridView1_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
        {
            var index = e.DataRow.RowIndex;
            //Get the column index value
            var columnIndex = e.DataColumn.ColumnIndex;
            //Get the cell value           
            var cellValue = dataGridView1.View.GetPropertyAccessProvider().GetValue(e.DataRow.RowData, e.DataColumn.GridColumn.MappingName);
           
        }
I am getting this error when I accidentally click the datagridview header. could somebody help me resolve this?


3 Replies 1 reply marked as answer

MA Mohanram Anbukkarasu Syncfusion Team July 17, 2020 11:59 AM UTC

Hi Mark, 
 
Thanks for contacting Syncfusion support.  

CellClick event will be raised even when clicking on the column header cells. But the e.DataRow.RowData will be null for the header row and the cell value will will also be null which may leads to the exception. It can be resolved by getting the cell value only for the DefaultRow and You can get the value resides in the header cell by getting the HeaderText of the clicked column as shown in the following code example. 

Code example :  

sfDataGrid1.CellClick += SfDataGrid1_CellClick; 
 
private void SfDataGrid1_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e) 
{ 
    if (e.DataRow.RowType == RowType.DefaultRow) 
    { 
        var index = e.DataRow.RowIndex; 
        var columnIndex = e.DataColumn.ColumnIndex; 
        var cellValue = sfDataGrid1.View.GetPropertyAccessProvider().GetValue(e.DataRow.RowData, e.DataColumn.GridColumn.MappingName); 
    } 
    else if(e.DataRow.RowType == RowType.HeaderRow) 
    { 
        var cellValue = e.DataColumn.GridColumn.HeaderText; 
    } 
} 


Please let us know if you require further assistance from us. 

Regards, 
Mohanram A. 


Marked as answer

MJ Mark Jayvee July 26, 2020 06:01 AM UTC

thank you, this solve my problem


MA Mohanram Anbukkarasu Syncfusion Team July 27, 2020 08:18 AM UTC

Hi Mark, 

Thanks for the update. 

We are glad to know that the provided solution worked at your end. Please let us know if you have any further queries on this. We are happy to help you. 

Regards, 
Mohanram A. 


Loader.
Up arrow icon