Home Forums WinForms controls Xceed Grid for WinForms Saving Grid’s layout Reply To: Saving Grid’s layout

User (Old forums)
Member
Post count: 23064

Such a function is not in the grid. But I’ve done this before. Below you can find 2 functions: 1 to get a string of XML from the layout, and 1 to load the string of XML and set the layout.
<code>
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)
{
string sort;
if (column.SortDirection == Xceed.Grid.SortDirection.Ascending) sort = “Ascending”;
else if (column.SortDirection == Xceed.Grid.SortDirection.Descending) sort = “Descending”;
else sort = “None”;
builder.AppendFormat(“<column name='{0}’ width='{1}’ visibleIndex='{2}’ sort='{3}’ sortIndex='{4}’/>”
, column.FieldName, column.Width, column.VisibleIndex, sort, column.SortIndex);
}
}
builder.Append(“</grid>”);
return builder.ToString();
}

void SetGridColumnsLayoutFromXml(Xceed.Grid.GridControl grid, string xmlFragment)
{
grid.BeginInit();
System.Collections.SortedList sortedColumns = new System.Collections.SortedList();
System.Collections.SortedList sortDirections = new System.Collections.SortedList();
int sortIndex;
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(xmlFragment, System.Xml.XmlNodeType.Element, null);
while(reader.Read())
if (reader.NodeType == System.Xml.XmlNodeType.Element && 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”));
}
}
}
reader.Close();
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);
}
grid.EndInit();
}
</code>

Imported from legacy forums. Posted by Tommy (had 530 views)