Remove a property/column from a generic list

后端 未结 2 1064
夕颜
夕颜 2020-12-16 22:18

Due to some reason I cannot change the query so I have to do this in C#.

I have a class:

public class myClass
{
    int id { get; set; }
    string n         


        
2条回答
  •  萌比男神i
    2020-12-16 22:38

    I'm not sure you should solve your issue in the Data, but rather it's a presentation problem. In which control do you want to display it ? Let's say you display it in DataGrid with AutoGenerateColumns=True, then you can 1) loop on columns/properties 2) for each column/property see if all property values for all rows are null and if so set column's visibility to Collapsed. If you generate your columns by yourself it's even simpler : only add columns when content is not null for all rows.
    If your DB content is dynamic, you might want to bind each row's visibility to a property that would state wether all rows are null or not for that property. Depending on how generic you want your code to be, the code might be very different, and in case you want to have generic solution, using Reflection to retrieve/get/set properties might be of some use.

提交回复
热议问题