We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to save column order and column width preferences in WinForms GridControl and GridDataBoundGrid?

Platform: WinForms |
Control: GridDataBoundGrid(Classic) |
Published Date: February 24, 2015 |
Last Revised Date: February 10, 2020

Save column order and column width

When a GridControl has serialization support through its GridModel.LoadBinary / GridModel.SaveBinary or GridModel.LoadSoap / GridModel.SaveSoap. Currently this is not supported in GridDataBoundGrid.

When you want to save column position and widths, then use .NET Frameworks serialization support.

C#

void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   try
   {
      //Serialize out the row heights & column widths
      IFormatter formatter = new BinaryFormatter();
      Stream stream = new FileStream("MySizes.bin", FileMode.Create, FileAccess.Write, FileShare.None);
      //Handle the mapping names
      GridBoundColumnsCollection col = (GridBoundColumnsCollection)this.gridDataBoundGrid1.GridBoundColumns;
      if(col.Count == 0)
         col = this.gridDataBoundGrid1.Binder.InternalColumns;
         int nCols = col.Count;
         string[] a = new string[nCols];
         int i = 0;
         foreach (GridBoundColumn c in col)
         a[i++] = c.MappingName;
         formatter.Serialize(stream, a);
         formatter.Serialize(stream, this.gridDataBoundGrid1.Model.ColWidths.Dictionary);
         stream.Close();
   }
   catch (Exception ex)
   { }
}
private void Form1_Load(object sender, EventArgs e)
{
   initializegrid();
}
public void initializegrid()
{
    if (File.Exists("MySizes.bin"))
    {
       try
       {
          IFormatter formatter = new BinaryFormatter();
          Stream stream = new FileStream("MySizes.bin", FileMode.Open, FileAccess.Read, FileShare.None);
          try
          {
             this.gridDataBoundGrid1.BeginUpdate();
             //handle the mappingnames
             GridBoundColumnsCollection col = (GridBoundColumnsCollection)this.gridDataBoundGrid1.GridBoundColumns.Clone();
             if (col.Count == 0)
                col = this.gridDataBoundGrid1.Binder.InternalColumns;
                string[] a = (string[])formatter.Deserialize(stream);
                this.gridDataBoundGrid1.GridBoundColumns.Clear();
                foreach (string s in a)
                {
                   GridBoundColumn c = col[s];
                   this.gridDataBoundGrid1.GridBoundColumns.Add(c);
                }
                this.gridDataBoundGrid1.Model.ColWidths.Dictionary = (Syncfusion.Windows.Forms.Grid.GridRowColSizeDictionary)formatter.Deserialize(stream);
          }
          finally
          {
             this.gridDataBoundGrid1.EndUpdate();
             this.gridDataBoundGrid1.Refresh();
             stream.Close();
          }
       }
       catch (Exception ex)
       { }
    }
}

VB

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
   Try
   'Serialize out the row heights & column widths
      Dim formatter As IFormatter = New BinaryFormatter()
      Dim stream As Stream = New FileStream("MySizes.bin", FileMode.Create, FileAccess.Write, FileShare.None)
      'Handle the mapping names
      Dim col As GridBoundColumnsCollection = CType(Me.gridDataBoundGrid1.GridBoundColumns, GridBoundColumnsCollection)
      If col.Count = 0 Then
        col = Me.gridDataBoundGrid1.Binder.InternalColumns
      End If
      Dim nCols As Integer = col.Count
      Dim a(nCols - 1) As String
      Dim i As Integer = 0
      For Each c As GridBoundColumn In col
        a(i) = c.MappingName
        i += 1
      Next c
      formatter.Serialize(stream, a)
      formatter.Serialize(stream, Me.gridDataBoundGrid1.Model.ColWidths.Dictionary)
      stream.Close()
      Catch ex As Exception
      End Try
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
   initializegrid()
End Sub
Public Sub initializegrid()
    If File.Exists("MySizes.bin") Then
      Try
         Dim formatter As IFormatter = New BinaryFormatter()
         Dim stream As Stream = New FileStream("MySizes.bin", FileMode.Open, FileAccess.Read, FileShare.None)
         Try
            Me.gridDataBoundGrid1.BeginUpdate()
            'handle the mappingnames
            Dim col As GridBoundColumnsCollection = CType(Me.gridDataBoundGrid1.GridBoundColumns.Clone(), GridBoundColumnsCollection)
            If col.Count = 0 Then
              col = Me.gridDataBoundGrid1.Binder.InternalColumns
            End If
            Dim a() As String = CType(formatter.Deserialize(stream), String())
            Me.gridDataBoundGrid1.GridBoundColumns.Clear()
            For Each s As String In a
               Dim c As GridBoundColumn = col(s)
               Me.gridDataBoundGrid1.GridBoundColumns.Add(c)
            Next s
            Me.gridDataBoundGrid1.Model.ColWidths.Dictionary = CType(formatter.Deserialize(stream), Syncfusion.Windows.Forms.Grid.GridRowColSizeDictionary)
 Finally
              Me.gridDataBoundGrid1.EndUpdate()
              Me.gridDataBoundGrid1.Refresh()
              stream.Close()
           End Try
           Catch ex As Exception
           End Try
       End If
End Sub

Samples:

C#: Save_column_width_and_position

VB: Save_column_width_and_position

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon