Get cellValue from SfDataGrid if Value is NULL

Hello again,

This time I'm trying to fill a form with values from datagrid when user clicks on Row.

Everything is working, except if values are null in Database.

So, here's my CellClick Event:

private void data_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
{
    int rowIndex = e.DataRow.RowIndex;
    var recordIndex = data.TableControl.ResolveToRecordIndex(rowIndex);
    var record1 = data.View.Records.GetItemAt(recordIndex);

    var ID = data.Columns[0].MappingName;
    var Anomalia = data.Columns[1].MappingName;
    var Descricao = data.Columns[2].MappingName;
    var DataC = data.Columns[3].MappingName;
    var Comentarios = data.Columns[4].MappingName;

    DetailID = Convert.ToInt32(record1.GetType().GetProperty(ID).GetValue(record1, null));
    CmbAnomalia.Text = record1.GetType().GetProperty(Anomalia).GetValue(record1, null).ToString();
    TxtDescAnomalia.Text = record1.GetType().GetProperty(Descricao).GetValue(record1, null).ToString() ?? null;
    DataCorrecao.Value = (DateTime?)record1.GetType().GetProperty(DataC).GetValue(record1, null);
//This is where I get Error (Object not set to an instance of an object
//But if there's no value in database (instead of NULL), I get no error
    TxtComentarios.Text = record1.GetType().GetProperty(Comentarios).GetValue(record1, null).ToString() ?? null;
}

Any workaround to avoid "Object not set to an instance of an object error?

Thanks

2 Replies

CF Carlos Ferreira August 24, 2018 10:49 PM UTC

Well, I resolved my problem with this...

TxtComentarios.Text = record1.GetType().GetProperty(Comentarios).GetValue(record1, null) == null ? "" : record1.GetType().GetProperty(Comentarios).GetValue(record1, null).ToString();

Thanks anyway!


AA Arulraj A Syncfusion Team August 27, 2018 05:35 AM UTC

Hi Carlos, 

Thanks for the update. 

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

Regards,  
Arulraj A  


Loader.
Up arrow icon