mardi 6 décembre 2016

C# LINQ select property if it has a custom attribute

I have a list of StudentViewModel object. I am binding this list with a DataGridView, and the column generatation is set to automatic according to the bound model's properties.

   public async Task LoadGridView()
    {
        Tuple<List<StudentViewModel>, int> result = await App.StudentService.SearchAsync(studentRequestModel);
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = result.Item1;
    }

In the StudentViewModel, I have decorated some of the properties with a custom attribute IsViewable.

[AttributeUsage(AttributeTargets.Property)]
public class IsViewable: Attribute
{
    public bool Value { get; set; }
}

usage:

        [IsViewable(Value = true)]
        public string Name { get; set; }

Idea is, just before binding with the UI Control, I want to filter the list and make a new list of anonymous object so that my grid will be populated with only selected properties.

enter image description here

Note: I don't want to create separate view models specific to Grids. I will refactor it if it creates performance issues.





Aucun commentaire:

Enregistrer un commentaire