10 Answers, 1 is accepted
I hope you are trying to save the run time settings of RadGrid. If so go through the following help document and see whether it helps.
Saving grid settings on a per user basis
Sincerely yours,
Pavlina
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Thanks
Guido
----------------------------------------
private
void PagePreRender() {
if (!IsPostBack) { PopulateFilterBoxes(); }
}
private
void PopulateFilterBoxes()
{
GridFilteringItem gfi = (GridFilteringItem) rgEntityList.MasterTableView.GetItems(GridItemType.FilteringItem)[0];
string allExpressions = rgEntityList.MasterTableView.FilterExpression;
string[] expressions = allExpressions.Split(new []{" AND "}, StringSplitOptions.RemoveEmptyEntries);
foreach (string exp in expressions) {
Match m = Regex.Match(exp, @"\[(.+)\] .* \'\%(.+)\%'");
string dataField = m.Groups[1].Value;
string value = m.Groups[2].Value;
GridBoundColumn column = null;
foreach (GridColumn gc in rgEntityList.Columns) {
if (!(gc is GridBoundColumn)) { continue; }
GridBoundColumn gbc = (GridBoundColumn) gc;
if (dataField.Equals(gbc.DataField)) {
column = gbc;
break;
}
}
if (column == null) continue;
TableCell td = gfi[column.UniqueName];
TextBox tb = (TextBox) td.Controls[0];
tb.Text = value;
}
}
Is there a way to keep the value of the filter in the column's textbox? I noticed you never got back to Guido on this. I'd like to see the official way to do this since I'm experiencing the same issue.
Here's the official code that I'm using (the same code you linked to earlier in this thread):
public class GridSettingsPersister |
{ |
private RadGrid gridInstance; |
public GridSettingsPersister(RadGrid gridInstance) |
{ |
this.gridInstance = gridInstance; |
} |
//this method should be called on Render |
public string SaveSettings() |
{ |
object[] gridSettings = new object[7]; |
int columnsLength = gridInstance.MasterTableView.Columns.Count + gridInstance.MasterTableView.AutoGeneratedColumns.Length; |
ArrayList allColumns = new ArrayList(columnsLength); |
allColumns.AddRange(gridInstance.MasterTableView.Columns); |
allColumns.AddRange(gridInstance.MasterTableView.AutoGeneratedColumns); |
/****************** Save filters *******************/ |
gridSettings[3] = (object)gridInstance.MasterTableView.FilterExpression; |
//save the visible/displayed columns settings and current filter value/current filter function |
ArrayList visibleColumns = new ArrayList(columnsLength); |
ArrayList displayedColumns = new ArrayList(columnsLength); |
Pair[] columnFilter = new Pair[columnsLength]; |
int i = 0; |
foreach (GridColumn column in allColumns) |
{ |
columnFilter[i] = new Pair(column.CurrentFilterFunction, column.CurrentFilterValue); |
visibleColumns.Add(column.Visible); |
displayedColumns.Add(column.Display); |
i++; |
} |
gridSettings[4] = visibleColumns; |
gridSettings[5] = displayedColumns; |
gridSettings[6] = columnFilter; |
/*****************************************************/ |
LosFormatter formatter = new LosFormatter(); |
StringWriter writer = new StringWriter(); |
formatter.Serialize(writer, gridSettings); |
return writer.ToString(); |
} |
//this method should be called on PageInit |
public void LoadSettings(string settings) |
{ |
LosFormatter formatter = new LosFormatter(); |
StringReader reader = new StringReader(settings); |
object[] gridSettings = (object[])formatter.Deserialize(reader); |
int columnsLength = this.gridInstance.MasterTableView.Columns.Count + this.gridInstance.MasterTableView.AutoGeneratedColumns.Length; |
ArrayList allColumns = new ArrayList(columnsLength); |
allColumns.AddRange(this.gridInstance.MasterTableView.Columns); |
allColumns.AddRange(this.gridInstance.MasterTableView.AutoGeneratedColumns); |
/****************** Load filter expression *******************/ |
this.gridInstance.MasterTableView.FilterExpression = (string)gridSettings[3]; |
//Load visible/displayed columns and their current filter values/current filter functions |
ArrayList visibleCols = (ArrayList)gridSettings[4]; |
ArrayList displayedColumns = (ArrayList)gridSettings[5]; |
Pair[] columnFilter = (Pair[])gridSettings[6]; |
int j = 0; |
foreach (GridColumn column in allColumns) |
{ |
column.CurrentFilterFunction = (GridKnownFunction)columnFilter[j].First; |
column.CurrentFilterValue = (string)columnFilter[j].Second; |
column.Visible = (bool)visibleCols[j]; |
column.Display = (bool)displayedColumns[j]; |
j++; |
} |
/***************************************************************/ |
} |
} |
I am calling Loadsettings on PageInit, but what I'm seeing is that the variable allColumns is being returned with 0 columns because my columns are autogenerated and apparently at this point, they are not accessible.
If I call Loadsettings from the RadGrid's prerender method, allColumns is returned with the correct number of columns, but I don't seem to be able to successfully set column filter values at that time.
Suggestions?
Thank you,
Eric Skaggs
Unfortunately we can not determine the exact cause for the issue you encountered without a working application which demonstrate it. At this point I will ask you to open a formal support ticket and send us a simple working project with reproduced this erroneous behavior. We will examine it in detail and will get back to you with our findings.
Regards,
Pavlina
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
(filterItem[column.UniqueName].Controls[0] as TextBox).Text = (string)columnFilter[a].Second;
With this:
if
(filterItem[column.UniqueName].Controls.Count > 0)
{
Control c = filterItem[column.UniqueName].Controls[0];
string text = (string)columnFilter[a].Second;
if (c.GetType() == typeof(TextBox))
{
((
TextBox)c).Text = text;
}
else if (c.GetType() == typeof(RadNumericTextBox))
{
((
RadNumericTextBox)c).Text = text;
}
}
Thank you for sharing your solution in this forum thread - thus you can help other community members which search similar type of functionality.
Greetings,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
I have a radgrid that is created fully in code. I used this code snip, and it works, only the first time it has a strange effect, after postback all is ok. any one a idea what i must do?
I create the grid in the Init, save settings it in load (if isPostBack) and load settings it in Prerender (if not is postback)
Regards,
Erik
Erik