I have a RadGrid that's binding to a list of abstract classes. However, some properties of some of the child classes have to be exposed in the grid (and left blank if they don't exist):
Class definitions and binding code:
Display of the grid works great. However, when I attempt to filter or sort on a column that exists in one of the child classes, I get an error:
Is there a simple way to go about fixing this? I have a few workarounds in mind (creating a view of the data and binding to that, or adding virtual properties to the Generic class), but I'd rather do this as cleanly and with as little overhead as possible.
Thanks!
<telerik:RadGrid runat="server" ID="rgStuff" AllowSorting="true" AllowFilteringByColumn="true" OnNeedDataSource="rgStuff_NeedDataSource" AutoGenerateColumns="False"> <MasterTableView> <Columns> <telerik:GridBoundColumn HeaderText="Generic1" DataField="Generic1"></telerik:GridBoundColumn> <telerik:GridBoundColumn HeaderText="Generic2" DataField="Generic2"></telerik:GridBoundColumn> <telerik:GridBoundColumn HeaderText="Bob1" DataField="Bob1"></telerik:GridBoundColumn> <telerik:GridBoundColumn HeaderText="Bob2" DataField="Bob2"></telerik:GridBoundColumn> <telerik:GridBoundColumn HeaderText="Steve1" DataField="Steve1"></telerik:GridBoundColumn> <telerik:GridBoundColumn HeaderText="Steve2" DataField="Steve2"></telerik:GridBoundColumn> </Columns> </MasterTableView></telerik:RadGrid>Class definitions and binding code:
class Generic{ public string Generic1 { get; set; } public string Generic2 { get; set; }}class Bob : Generic{ public string Bob1 { get; set; } public string Bob2 { get; set; }}class Steve : Generic{ public string Steve1 { get; set; } public string Steve2 { get; set; }}public partial class InheritanceTest : System.Web.UI.Page{ protected void rgStuff_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) { rgStuff.DataSource = new List<Generic>() { new Bob() { Generic1 = "First", Generic2 = "Bob", Bob1 = "Bob1-1", Bob2 = "Bob1-2" }, new Bob() { Generic1 = "Second", Generic2 = "Bob", Bob1 = "Bob2-1", Bob2 = "Bob2-2" }, new Steve() { Generic1 = "First", Generic2 = "Steve", Steve1 = "Steve1-1", Steve2 = "Steve1-2" }, new Steve() { Generic1 = "Second", Generic2 = "Steve", Steve1 = "Steve2-1", Steve2 = "Steve2-2" } }; }}Display of the grid works great. However, when I attempt to filter or sort on a column that exists in one of the child classes, I get an error:
No property or field 'Bob1' exists in type 'Generic'
Is there a simple way to go about fixing this? I have a few workarounds in mind (creating a view of the data and binding to that, or adding virtual properties to the Generic class), but I'd rather do this as cleanly and with as little overhead as possible.
Thanks!