Home Forums WinForms controls Xceed Grid for WinForms Saving Formatted Grids

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

    I am wondering if anyone has code to save a users formatted grid.
    My project is in VB.Net, and i would like to be able to save the format of the grid if it is manually changed at runtime.
    I was thinking of writing it to an XML file of some sort.

    Any help would be greatly appreciated!

    Imported from legacy forums. Posted by Tacksi (had 2984 views)

    Xceed Support
    Member
    Post count: 5658

    Here is a rough idea of what you could do:

    <code>
    private void SaveGridSettings( GridControl grid, string filename )
    {
    try
    {
    XmlTextWriter xmlTW = new XmlTextWriter( filename, null );
    xmlTW.Formatting = Formatting.Indented;
    xmlTW.WriteStartDocument();
    xmlTW.WriteStartElement( “GridControl” );

    xmlTW.WriteStartElement( “Columns” );
    xmlTW.WriteString( grid.Columns.Count.ToString() );
    foreach( Column col in grid.Columns )
    {
    xmlTW.WriteStartElement( col.Title );

    xmlTW.WriteStartElement( “Width” );
    xmlTW.WriteString( col.Width.ToString() );
    xmlTW.WriteEndElement(); //End Width

    xmlTW.WriteStartElement( “VisibleIndex” );
    xmlTW.WriteString( col.VisibleIndex.ToString() );
    xmlTW.WriteEndElement(); //End VisibleIndex

    xmlTW.WriteEndElement(); //End col.Title
    }
    xmlTW.WriteEndElement(); //End Columns

    xmlTW.WriteStartElement( “DataRows” );
    xmlTW.WriteString( grid.DataRows.Count.ToString() );
    foreach( Xceed.Grid.DataRow row in grid.DataRows )
    {
    xmlTW.WriteStartElement( “DataRow” + row.Index.ToString() );

    xmlTW.WriteStartElement( “Height” );
    xmlTW.WriteString( row.Height.ToString() );
    xmlTW.WriteEndElement(); //End Height

    xmlTW.WriteStartElement( “Cells” );
    foreach( Cell cell in row.Cells )
    {
    xmlTW.WriteStartElement( “Cell” + cell.ParentColumn.Index.ToString() );

    xmlTW.WriteStartElement( “BackColor” );
    xmlTW.WriteString( cell.BackColor.Name );
    xmlTW.WriteEndElement(); //End BackColor

    xmlTW.WriteEndElement(); //End cell.ParentColumn.Index
    }
    xmlTW.WriteEndElement(); //End Cells

    xmlTW.WriteEndElement(); //End row.Index
    }
    xmlTW.WriteEndElement(); //End DataRows

    xmlTW.WriteEndElement(); //End GridControl

    xmlTW.WriteEndDocument();
    xmlTW.Flush();
    xmlTW.Close();
    }
    catch( Exception ex )
    {
    MessageBox.Show( ex.ToString() );
    }
    }

    private void LoadGridSettings( ref GridControl grid, string filename )
    {
    try
    {
    XmlTextReader xmlTR = new XmlTextReader( new StreamReader( filename ) );

    xmlTR.ReadStartElement(); //GridControl
    xmlTR.ReadStartElement(); //Columns
    int numberOfColumns = xmlTR.ReadContentAsInt();

    if( numberOfColumns == grid.Columns.Count )
    {
    foreach( Column col in grid.Columns )
    {
    xmlTR.ReadStartElement(); //col.Title
    xmlTR.ReadStartElement(); //Width

    col.Width = xmlTR.ReadContentAsInt();

    xmlTR.ReadEndElement(); //End Width

    xmlTR.ReadStartElement(); //VisibleIndex
    col.VisibleIndex = xmlTR.ReadContentAsInt();
    xmlTR.ReadEndElement(); //End VisibleIndex

    xmlTR.ReadEndElement(); //End col.Title
    }
    xmlTR.ReadEndElement(); //End Columns
    }
    else
    {
    throw new Exception( “Different number of columns” );
    }

    xmlTR.ReadStartElement(); //DataRows
    int numberOfDataRows = xmlTR.ReadContentAsInt();

    if( numberOfDataRows == grid.DataRows.Count )
    {
    foreach( Xceed.Grid.DataRow row in grid.DataRows )
    {
    xmlTR.ReadStartElement(); //row.Index
    xmlTR.ReadStartElement(); //Height

    row.Height = xmlTR.ReadContentAsInt();

    xmlTR.ReadEndElement(); //End Height

    xmlTR.ReadStartElement(); //Cells
    foreach( Cell cell in row.Cells )
    {
    xmlTR.ReadStartElement(); //Cell.Index
    xmlTR.ReadStartElement(); //BackColor

    cell.BackColor = Color.FromName( xmlTR.ReadContentAsString() );

    xmlTR.ReadEndElement(); //Cell.Index
    xmlTR.ReadEndElement(); //BackColor
    }
    xmlTR.ReadEndElement(); //End Cells

    xmlTR.ReadEndElement(); //End row.Index
    }

    xmlTR.ReadEndElement(); //End DataRows
    }
    else
    {
    throw new Exception( “Different number of datarows” );
    }

    xmlTR.ReadEndElement(); //End GridControl
    xmlTR.Close();
    }
    catch( Exception ex )
    {
    MessageBox.Show( ex.ToString() );
    }
    }
    </code>

    Imported from legacy forums. Posted by CharlesB (had 266 views)

    User (Old forums)
    Member
    Post count: 23064

    Does Xceed provide a serialization format for grid settings already? I’m in a similar situation but I don’t really care about the format of the settings data.

    Imported from legacy forums. Posted by mcw0933 (had 470 views)

    Xceed Support
    Member
    Post count: 5658

    The Xceed Grid does not provide a serialization format for the grid settings. That is why I posted this “idea of a procedure”.

    Imported from legacy forums. Posted by CharlesB (had 3993 views)

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