C# grid DataSource polymorphism

前端 未结 7 1745
我在风中等你
我在风中等你 2021-01-06 06:28

I have a grid, and I\'m setting the DataSource to a List. What I want is to have the list bind to the underlying type, and disply

7条回答
  •  無奈伤痛
    2021-01-06 06:54

    You'll need to use a Grid template column for this. Inside the template field you'll need to check what the type of the object is and then get the correct property - I recommend creating a method in your code-behind which takes care of this. Thus:

    
        
            <%#GetUserSpecificProperty(Container.DataItem)%>
        
    
    

    In your code-behind:

    protected string GetUserSpecificProperty(IListItem obj) {
        if (obj is User) {
            return ((User) obj).UserSpecificField
        } else if (obj is Location) {
            return ((Location obj).LocationSpecificField;
        } else { 
            return "";
        }
    }
    

提交回复
热议问题