Binding to a List<string> using DataBinder.Eval
I have the following object:
CrossTabDataObject
{
string RowName{get;set;};
int RowId{get;set;}
开发者_开发技巧List<string> CellContents = new List <string>();
//Constructor..... etc.
}
I'm building a dynamic crosstab grid using GridView in asp.net 3.5
I wish to bind to CellContents[0] for dynamic column 1, CellContents[1] for dynamic column 2 (dynamic column 0 is the RowName field from the CrossTabDataObject) etc. I am using:
object boundValueObj = null;
Control ctrl = (Control)sender;
IDataItemContainer dataItemContainer = (IDataItemContainer)ctrl.NamingContainer;
boundValueObj = DataBinder.Eval(dataItemContainer.DataItem, strSelectedID);
This code is in the InstantiateIn function of the gridview as I'm creating drop down list templates in each cell of the crosstab.
I have code (not shown) that sets strSelectedID depending on the column being created.
When strSelectedID is equal to "RowName" for dynamic column [0] the DataBinder.Eval function works fine and sets boundValueObj as expected. The problem comes when strSelectedID is set to "CellContents[n]" where n is the Column Index.
DataBinder.Eval only works with properties and fields of objects. How do I get around this?
Thanks,
Rich.
OK - stupid mistake by me!
Changing:
CrossTabDataObject
{
string RowName{get;set;};
int RowId{get;set;}
List<string> CellContents = new List <string>();
//Constructor..... etc.
}
to:
CrossTabDataObject
{
string RowName{get;set;};
int RowId{get;set;}
List<string> CellContents{get;set;}
//Constructor
public CrossTabDataObject()
{
CellContents = new List<string>();
}
}
made all the difference. In other words I made CellContents a property of the class.
Rich.
精彩评论