Hi,
I am using GridView. I add columns in the grid dynamically at run-time. Here is the scenario.
1. I have zero columns in the grid.
2. I add a column in the grid. e.g. with header "Employee Name". then refresh data source.
using myGrid.Columns.Add();
using myGrid.ItemSource = xyz();
3. I apply a filter on the grid e.g. "Starts with" 'Man'
4. I add another column to the grid e.g. with header "Employee Phone", then refresh data source.
using myGrid.Columns.Add();
using myGrid.ItemSource = xyz();
5. The that i applied on the "Employee Name" column disappears.
How can i retain the filters when adding new columns in the grid dynamically?.
Thanks,
Mansoor Nadeem.
5 Answers, 1 is accepted
Currently this is a bug in our implementation. We are aware of it and will try to address it in a future release.
However there is a work-around solution that can be used right now. I'm attaching a sample project that shows how to achieve this.
Hope this helps.
Sincerely yours,
Stefan Dobrev
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
I integrated the sample code that you provided with my application. When i try to add a new column in the report after applying filter to an existing column, it is throwing an exception 'Unable to cast object of type TempType66788284' to type 'TempType19831799'. I am unable to fiqure out why is this happening.
-> below is the code for adding a new column in the grid
private GridViewDataColumn CreateColumn(Type columnType, string dataPath, string HeaderText)
{
GridViewDataColumn myColumn = new GridViewDataColumn();
myColumn.DataType = columnType;
myColumn.UniqueName = dataPath;
myColumn.DataMemberBinding =
new System.Windows.Data.Binding(dataPath);
myColumn.HeaderText = HeaderText;
return myColumn;
}
-> This methods updates datasource and creates a new column
public void updateDummyDataSource(RB.Objects.ReportColumn ColumntoOperateOn)
{
List<FieldFilterDescription> filters = this.GetActiveFilters().ToList();
CreateColumn(ColumntoOperateOn);
TheGrid.ItemsSource = GenerateDummyData(ColumnsArray).ToDataSource();
this.ApplyActiveFilters(filters);
}
-> DataSource is IEnumerable<IDictionary>
private IEnumerable<IDictionary> GenerateDummyData(IList<RB.Objects.ReportColumn> ColumnArray)
-> The exception comes in your method 'ApplyActiveFilters' on the line written below
if (index >= 0)
{
compositeFilterDescription.FilterDescriptions[index] = filter; //EXCEPTION COMES HERE
continue;
}
a. Can you please help me out on the exception above.
b. I also need to save these filters which i already did. And recreate them after picking them from database when the page is reopened. Can you give me some sample code on how to do that?
c. Is there some way to eliminate the list of filters with checkboxes from the filter list. As it is dependent on the current data.
Thanks,
Mansoor Nadeem.
The problem that you describes comes from the fact that every time you bind the grid with different type of objects. I suppose you are generating these types on the fly. Despite of this I have prepared a modified version of the sample that handles your scenario.
Regarding your other questions:
- You can check this blog post that shows how you can persist the filtering of the grid.
- Currently there is no easy way to hide the distinct values list box. However I have prepared a workaround that you can also find in the attached archive.
Hope this helps.
Kind regards,
Stefan Dobrev
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Thanks for your great support. The filters issue has almost been resolved. A couple of last tweaks are required.
a. Due to my project requirements, i need to hide a column but retain the filters :). The code that you sent me throws an exception when i use "Grid.Columns[i].isvisible = false;". Here is the scenario. I apply filters on a column and then hide it. It throws an exception when i tries to retain the filter of that column (your code for saving current filters). Is there any workaround, can I set the width of the column to '0' and freeze resizing.
b. I also need to update the header of a column. I do it by Grid.Columns[i].ColumnHeader = "ALL COUNTIES". The changes are not reflected unless i reassign the data source. The code that you sent me for filters also throws exception when i change the header and then apply filter on a column.
c. Regarding the hide distinct values of filters. Intially i have zero columns in a grid. I add a column. I assign itemsource. The filtersDescription are null till this point, so i cannot execute the code you gave me to hide distinct values. However when i add another column in the grid and reassign itemsource the filters are intialized and then i can execute your code. Any workarounds whould be great.
Thanks,
Mansoor Nadeem.
Please check out the answers to your questions in the support ticket you have created.
All the best,
Stefan Dobrev
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.