Home Forums WinForms controls Xceed Grid for WinForms Saving Grid’s layout

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13145 |

    Hi there,
    I am wondering if there is any way of saving the grid’s column’s layout in a file and loading it at runtime. My application requires the individual user to customize the grid columns (re-arrange) the order and save them, so that when they log in next time the same layout is displayed. Is there any built in function in the grid to acheive this or will i have to manually store the layout in an xml file and load it at runtime..

    Thanks in advance,
    Sharad

    Imported from legacy forums. Posted by Sharad (had 3952 views)

    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)

    User (Old forums)
    Member
    Post count: 23064

    Thank a mil for you help tommy..really appreciated!!

    Imported from legacy forums. Posted by Sharad (had 3423 views)

    Xceed Support
    Member
    Post count: 5658

    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)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.