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.
Unfortunately, activation email could not send to your email. Please try again.

How to retain the column width after changing the ColumnSizer at run time?

In SfDataGrid, the ColumnSizer property allows you to specify the column width, based on the data present in the cell and available width. You can define the ColumnSizer property either in the SfDataGrid or Column level.

When you change the ColumnSizer at run time, the column width is calculated based on the type of ColumnSizer.

The following screenshot displays the SfDataGrid loaded with few columns of default width (150) and rest of the widths are set explicitly,

Figure 1: Default View

Implement the following, before changing the ColumnSizer.

  • The previous column width should be stored in a separate List.
  • Set double. NAN value to all the columns before changing the ColumnSizer. Since, ColumnSizer is not applied to the columns when the column width is set explicitly. This is the default behavior of ColumnSizer.

You can achieve the above using the following code example.

C#

After changing the ColumnSizer as Auto at runtime, the SfDataGrid’s Columns width is calculated based on the Auto column sizer as illustrated in the following screenshot.

Figure 2: After changing the ColumnSizer as Auto

When you want to retain the column width that was loaded before the ColumnSizer changes, you can achieve it by using the stored List as illustrated in the following code example.

C#

The above code example restores the old width of the columns that is illustrated in the following screenshot.

Figure 3: Restore the old width to Columns

Sample Links

WPF

WRT

SilverLight

UWP

Article ID: Published Date: Last Revised Date: Platform: Control: .NET Framework:
3832 02/10/2015 02/05/2016 WinRT SfDataGrid 4.5
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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.