C# Datagridview responsive column width for any screen resolution

T.Zacks 3,996 Reputation points
2022-07-21T11:16:15.607+00:00

i want to work with datagridview in such a way that column width will increase based on size. whatever would be screen resolution my grid column width will be shown properly. please advise how to design grid column width in such a way.

i have read few articles on this but looking for best solution

https://stackoverflow.com/questions/15578945/datagridview-column-widths-as-percentage
https://social.msdn.microsoft.com/Forums/silverlight/en-US/062a2fc8-802d-4390-b2c8-ec73153e1911/column-width-in-percentage-for-datagrid?forum=silverlightcontrols

https://www.informit.com/articles/article.aspx?p=446453&seqNum=10
https://www.codeguru.com/dotnet/autosize-a-datagridview-to-fit/

Thanks

Developer technologies C#
{count} votes

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-21T15:04:52.41+00:00

    There are a few properties of DataGridViewColumn which play a role in the width of the column, including DataGridViewColumn.Width, DataGridViewColumn.AutoSizeMode, DataGridViewColumn.MinimumWidth, FillWight, DataGridView.AutoSizeColumnsMode.

    I can highlight 2-3 interesting scenario to to resize columns in larger screens:

    1. You want all the columns have equal size and fill the width of the grid, and grow equally if size of form changes: So set the AutoSizeMode of all the columns to Fill. 223313-1.gif
    2. You want the columns have different sizes, but fill the width of the grid and grow relatively when the size of form changes: So set the AutoSizeMode of all the columns to Fill, and set the proper FillWidth, which will be used as the weight of the column when resizing. 223344-2.gif
    3. You want all the columns keep their size, but a specific column (like last column) fill the width of grid when the size of form changes: Set the column size modes to something like None, but for that specific column set it to Fill. 223332-3.gif

    And you can also find some useful scenarios and settings about sizing of the columns and rows, including sizing based on the content, in the following doc:

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-07-21T12:37:44.823+00:00

    See if this works for you

    public static void ExpandColumns(this DataGridView source, bool sizable = true)  
    {  
        foreach (DataGridViewColumn col in source.Columns)  
        {  
            if (col.ValueType.Name != "ICollection`1")  
            {  
                col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;  
            }  
        }  
      
        if (!sizable) return;  
      
        for (int index = 0; index <= source.Columns.Count - 1; index++)  
        {  
            int columnWidth = source.Columns[index].Width;  
      
            source.Columns[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;  
            source.Columns[index].Width = columnWidth;  
        }  
      
      
    }  
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.