Articles in this section
Category / Section

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

3 mins read
Save column order and column width
When a WinForms 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

Conclusion

I hope you enjoyed learning about how to how to save column order and column width preferences in WinForms GridControl and GridDataBoundGrid.

You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations documentation and how to quickly get started for configuration specifications.  You can also explore our WinForms DataGrid  example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied