Doing someting similar to the code in the posting for "Building a Full-Featured Custom DataGrid Control" using datagrids and having a duece of a time getting past one problem.
The error encountered is System.InvalidCastException: Specified cast is not valid. I'm using the same code as up above for setting up the databinding for an item template in a datagrid. If I use a control file I have no problems with binding the data (from my SQL Server database). But for some reason I can't assign the databinding using a code behind file . Here's the code, just like up above as far as I can tell.
LiteralControl lc = (LiteralControl) sender;
DataGridItem container = (DataGridItem) lc.NamingContainer;
lc.Text = ((DataRowView) container.DataItem) [columnName].ToString();
Again, I have no problems binding to my itemtemplate column using other methods, so I'm wrather sure it's not a data issue. I have seen mention on MSDN of this type of problem possibly being related to control tree hierarchy being re-ordered at this address
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q327287 but I couldn't see how to fix it from what they gave as a solution. Their solution, to save you the trip, is this:
"Make sure that the control tree is re-created on postback in the same order that it was saved at the end of the previous request." My depth of .net knowledge doesn't go that deep.
Any ideas?
Thanks.
Stack trace is here.
[InvalidCastException: Specified cast is not valid.]
DataGridTemplate.OnDataBinding(Object sender, EventArgs e) +13
System.Web.UI.Control.OnDataBinding(EventArgs e) +66
System.Web.UI.Control.DataBind() +26
System.Web.UI.Control.DataBind() +86
System.Web.UI.Control.DataBind() +86
System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource) +169
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) +1411
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +49
System.Web.UI.WebControls.BaseDataList.DataBind() +23
Rock.BudgetDataGrid.datagridSetup(DataGrid datagrid) +914
ASP.CreateProfilePage4_aspx.Page_Load(Object sender, EventArgs e) in \CreateProfilePage4.aspx:28
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750