Is there a way to clear the viewstate for the grid in order to change the datasource from the code behind?
I have a requirement that the users are able to change the datasource being viewed in the grid at any time or basically recreate the grid with a new datasource. Currently I have EnableViewState = True in order to handle the user doing filtering,grouping,sorting..... This works well for a single dataset but appears to crash when changing the data being viewed because the grid isn't recognizing that the columns have changed. If I i disable the view state, changing the dataset works but I loose other functionality generally stored in the viewstate.
Scenario:
The user loads the grid displaying a small datatable with 4 columns, after reviewing the data they change to a different dataset with 20 columns.
On switching the dataset I have cleared all bands,rows,columns and behaviors from the original grid and created new bands,columns,behaviors for the new grid. When debugging the grid and new bands show the correct number of columns but they just don't rendor properly if EnableViewState is True. The second dataset renders in the original 4 columns if the keys match.
If the new dataset has predefined behaviors such as column sorting the grid crashes sbecause it trys to apply a behavior to the grid that the viewstate says doesn't exist.
[NullReferenceException: Object reference not set to an instance of an object.] Infragistics.Web.UI.GridControls.Sorting.AddHeaderImgToCaption(GridField column, SortDirection direction) +442 Infragistics.Web.UI.GridControls.Sorting.BehaviorEvents_PreRender(Object sender) +160 Infragistics.Web.UI.GridControls.RenderingContentHandler.Invoke(Object sender) +0 Infragistics.Web.UI.GridControls.GridRenderer.RenderContents(HtmlTextWriter writer) +169 Infragistics.Web.UI.GridControls.HierarchicalGridRenderer.RenderContents(HtmlTextWriter writer) +641 Infragistics.Web.UI.Framework.RunBot.HandleRenderContents(HtmlTextWriter writer, RendererBase renderer) +248 Infragistics.Web.UI.Framework.Data.HierarchicalDataBoundControlMain.Render(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +245 System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +314 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +47 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +245 System.Web.UI.Page.Render(HtmlTextWriter writer) +39 System
Thanks for any help or suggestions
Hello leas3c,
If you would like to clear the datasource, you should do as Dr Tone suggested.
Let me know if you need further assistance with this.
Regards,
Lyuba
Developer Support Engineer
Infragistics
www.infragistics.com/support
Hi,
I'm also facing the same issue. Even after clearing the datasource, viewstate of webdatagrid, its still giving me this error at render of control. This happens only when I do sorting then rebind of grid with different datasource. Please help. Its urgent.
at Infragistics.Web.UI.GridControls.Sorting.AddHeaderImgToCaption(GridField column, SortDirection direction) at Infragistics.Web.UI.GridControls.Sorting.BehaviorEvents_PreRender(Object sender) at Infragistics.Web.UI.GridControls.GridBehaviorEvents.RenderingContentHandler.Invoke(Object sender) at Infragistics.Web.UI.GridControls.GridRenderer.RenderContents(HtmlTextWriter writer) at Infragistics.Web.UI.Framework.RunBot.HandleRenderContents(HtmlTextWriter writer, RendererBase renderer) at Infragistics.Web.UI.Framework.Data.FlatDataBoundControl.Render(HtmlTextWriter writer) ....................
TIA
Where you able to find a solution? I am having the same problem but ClearDataSource is not helping.
Thanks!
I was having the same problem. For the record I am on 2011V1.
In my case, I was databinding to multiple data sources. All but one had the same schema/columns. The one that didn't was when I wanted to clear the WebDataGrid. In that case I would assign the grid's DataSource to null. Under some circumstance, when I had sorted on one of the columns, then assigned the DataSource to null, I would get the exception at AddHeaderImgToCaption.
I don't know why the code was doing this but something in the control (I believe) was trying to apply the sorting to the new DataSource (null) and it didn't have the column that had been sorted previously. That all makes sense...I guess.
For me, there were two solutions, either turn off sorting when I assigned the null DataSource (and turn it back on when the DataSource wasn't null), or clear the SortedColumns.
grid.Behaviors.Sorting.Enabled = false;
or
grid.Behaviors.Sorting.SortedColumns.Clear();
The difference being that if I just disable sorting, when I would enable it later, it would still try to sort on the same column that had been sorted previously on another data source. This was OK for my app (but may not be for yours) because when I was enabling it back on, the columns were the same as the last non-null DataSource.
Another approach would be to clear the SortedColumns. By doing that, sorting was still enabled but there was no state maintained as to which column was being sorted. This is the approach I ultimately took for my app because I didn't think it made sense to the user for the column to be sorted based on a previous data source.
I suspect that if you are binding to data sources with different schemas and setting AutoGenerateColumns to true, meaning they have different columns, then you should go with clearing the SortedColumns collection.
Based on my experience, calling ClearDataSource() does not clear the SortedColumns collection. Initially I felt that was a bug. Now, I am not sure it would be considered a bug. Which column is sorted on is not really part of the data source as long as the columns don't change between data sources.
I hope this helps someone.