Using reflection to update a variable column

There is a grid which shows all the rows of the entity table. On top of that a screen with a combobox in which all the colums of the entity is shown and next to that a textbox for the new value of these colums.

On button_click we want to update the selected column for the selected rows with the new values. To do that you will need to compare the name of the column with the one choosen in the combobox. Like this:

public void Update(int id, string columnName)

    // create entity object
    CentraalArtikelBestand cab = centraalArtikelBestandData.GetCentraalArtikelBestand().Where
        (a => a.CentraalArtikelBestandId == id).FirstOrDefault();
    // create propertyinfo object
    PropertyInfo propinfo = centraalArtikelBestandData.GetCentraalArtikelBestand().Where
        (a => a.CentraalArtikelBestandId == id).FirstOrDefault().GetType().GetProperties().Where
        (a => a.Name.Equals(column, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
    // update selected column
        propinfo.SetValue(cab, intValue.Value, null);
    // save dbContext





Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!