Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you databind an asp.net gridview to a field in a base class?

I have the following classes (pseudocode):

  • Item ( int Field1 )
  • ItemDetail : Item (int Field2, string field3)

If I set ItemDetail as the datasource for an asp.net gridview:

grid.DataSource = new List<ItemDetail>();
grid.DataBind();

Can I use Field1 in the GridView? If so, what is the correct DataBinder syntax? The following code blows up trying to cast to an Item:

<%# DataBinder.Eval(Container.DataItem, "Field1") %>

Thanks in advance for any help.

EDIT: And I'm a moron. I had copied the gridview and was calling a RowDataBound event handler for a different grid... Sorry to have wasted everyones time, but there is some good info here regardless if anyone has the same question. In the end, the public properties of the base class are binding correctly. Thanks!

like image 283
IronicMuffin Avatar asked Nov 26 '22 17:11

IronicMuffin


1 Answers

If your base class property is public, you shouldn't have an issue as long as you're also initializing it in your child constructors.

Also, I'm not sure if your example binding is pseudocode or not, but you have to bind a grid to an IListSource, IEnumerable, or IDataSource, so you need a Collection, List, IQueryable, etc. of ItemDetails.

like image 77
jwheron Avatar answered Dec 04 '22 01:12

jwheron