Home Forums WinForms controls Xceed Grid for WinForms Saving Formatted Grids Reply To: Saving Formatted Grids

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)