Home › Forums › WinForms controls › Xceed Grid for WinForms › Saving Grid’s layout › Reply To: Saving Grid’s layout
This post was very useful to me. For anybody that also wants to save the grouping, I’ve added that to Tommy’s very helpful code as well….cobbled together from other info on this site, but I couldn’t find it all in one place so I thought I’d drop it in here…
<code>
/// <summary>
/// Class for grid utility methods
/// </summary>
public class GridUtils
{
/// <summary>
/// Gets an XML representation of the grid layout.
/// </summary>
/// <param name=”grid”></param>
/// <returns></returns>
public static string GetXmlFromGridColumnsLayout(Xceed.Grid.GridControl grid)
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append(“<grid>”);
foreach (Xceed.Grid.Column column in grid.Columns)
{
if (column.Visible)
builder.AppendFormat(“<column name='{0}’ width='{1}’ visibleIndex='{2}’ sort='{3}’ sortIndex='{4}’/>”, column.FieldName, column.Width, column.VisibleIndex, column.SortDirection.ToString(), column.SortIndex);
}
foreach (Xceed.Grid.Group group in grid.GroupTemplates)
{
builder.AppendFormat(“<group groupby='{0}’ groupbyrowtitle='{1}’/>”, group.GroupBy, group.GroupByRowTitle);
}
builder.Append(“</grid>”);
return builder.ToString();
}
/// <summary>
/// Reads an XML saved representation of the grid layout and applies it to the grid
/// </summary>
/// <param name=”grid”></param>
/// <param name=”xmlFragment”></param>
public static void SetGridLayoutFromXML(Xceed.Grid.GridControl grid, string xmlFragment)
{
//Locals to store xml data read back in
System.Collections.SortedList sortedColumns = new System.Collections.SortedList();
System.Collections.SortedList sortDirections = new System.Collections.SortedList();
List<Group> lstGroups = new List<Group>();
#region read xml document back in
int sortIndex;
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(xmlFragment, System.Xml.XmlNodeType.Element, null);
try
{
while (reader.Read())
{
if (reader.NodeType == System.Xml.XmlNodeType.Element)
{
if (reader.Name == “column”)
{
Xceed.Grid.Column column = grid.Columns[reader.GetAttribute(“name”)];
if (column != null && column.Visible)
{
column.Width = Int32.Parse(reader.GetAttribute(“width”));
column.VisibleIndex = Int32.Parse(reader.GetAttribute(“visibleIndex”));
if (reader.GetAttribute(“sort”) == “Ascending” || reader.GetAttribute(“sort”) == “Descending”)
{
sortIndex = Int32.Parse(reader.GetAttribute(“sortIndex”));
sortedColumns.Add(sortIndex, column);
sortDirections.Add(sortIndex, reader.GetAttribute(“sort”));
}
}
}
else if (reader.Name == “group”)
{
string groupBy = reader.GetAttribute(“groupby”);
Xceed.Grid.Group group = new Group(groupBy);
group.GroupBy = groupBy;
group.GroupByRowTitle = reader.GetAttribute(“groupbyrowtitle”);
lstGroups.Add(group);
}
}
}
}
finally
{
reader.Close();
}
#endregion
#region update grid layout with read in data
grid.BeginInit();
try
{
//Add columns
grid.SortedColumns.Clear();
for (int i = 0; i < sortedColumns.Count; i++)
{
bool ascending = (sortDirections.GetByIndex(i).ToString() == “Ascending”);
grid.SortedColumns.Add(sortedColumns.GetByIndex(i) as Xceed.Grid.Column, ascending);
}
//Add grouping
grid.GroupTemplates.Clear();
foreach (Group group in lstGroups)
{
grid.GroupTemplates.Add(group);
}
grid.UpdateGrouping();
}
finally
{
grid.EndInit();
}
#endregion
}
}
</code>
Imported from legacy forums. Posted by Simon (had 1192 views)