question

ChrisEllis-5002 avatar image
0 Votes"
ChrisEllis-5002 asked ChrisEllis-5002 commented

DataGridView column resizing

Hi,

I'm using a DataGridView in a Windows Forms application. I have AllowUserToResizeColumns set to true. I am setting the DataSource to an instance of my own class that is derived from BindingList<object> that implements sorting. I am setting the the columns ReadOnly to true as well as the ReadOnly property of the DataGridView itself.

The grid fills perfectly and allows columns to be moved and/or sorted, but they cannot be resized by the user, and I cannot figure out what is causing this and how to fix it. They are auto-sized to the data to begin with, but the user ought to be able to resize them as they desire. There is one column (the non-column before the first column) that works as I would expect, allowing user resizing. All of the columns with data do not allow the user to resize them.

I would certainly appreciate if anyone could point me in the right direction.
Thanks in advance,
Chris

windows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I could post a small project that displays the problem if anyone is unsure of what I'm describing

0 Votes 0 ·
TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered ChrisEllis-5002 commented

Did you set the AutoSizeMode property?

DataGridViewColumn.AutoSizeMode Property

This will result in the column width cannot be set manually.

If not, please provide some details so that we can reproduce the current problem.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So, your response set me on the right track. I had the DataGridView.AutoSizeColumns property set to AllCells. I replaced that functionality with a one-time call to DataGridView.AutoResizeColumns(AllCells) after the data loads, and that got me to exactly where I wanted to go. So thank you!

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered

Hello,

After the DataGridView.DataSource has been set use the following language extension method which first sets each column to fit the widest value per column (which you now can not resize manually) then in the for sets the widest width follow by removing the restriction to not manually resize.

 public static class DataGridViewExtensions
 {
     /// <summary>
     /// Expand all columns and suitable for working with Entity Framework in regards to ICollection`1 column types.
     /// </summary>
     /// <param name="sender">DataGridView</param>
     /// <param name="sizable">Undue DataGridViewAutoSizeColumnMode.AllCells which makes manual resizing possible</param>
     public static void ExpandColumns([NotNull] this DataGridView sender, bool sizable = false)
     {
         foreach (DataGridViewColumn col in sender.Columns)
         {
             if (col.ValueType.Name != "ICollection`1")
             {
                 col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
             }
         }
    
         if (sizable)
         {
             for (int index = 0; index <= sender.Columns.Count - 1; index++)
             {
                 int columnWidth = sender.Columns[index].Width;
    
                 sender.Columns[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    
                 // Set Width to calculated AutoSize value:
                 sender.Columns[index].Width = columnWidth;
             }
         }
     }
 }

Call the above (not passing true bypasses the auto size mode),

 someDataGridView.ExpandColumns(true);





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.