DataGridView column auto adjust width and resizable
Asked Answered
E

7

9

I am wondering if it is possible to have a dataGridView column to adjust automatically its width to its content and at the same time being resizable by the user ?

Here what i have tried so far :

dataGridView.AllowUserToResizeColumns = true;
dataGridView.Columns[0].AutoSizeMode=DataGridViewAutoSizeColumnMode.DisplayedCells;
dataGridView.Columns[0].Resizable = DataGridViewTriState.True;

But, I am still unable to resize the column size manually.

If anyone already faced this issue or have any idea let me know.

Thanks.

El answered 18/7, 2013 at 14:27 Comment(2)
aren't DataGridView columns resizable by default? What happens when you remove all of this code? Can you resize them?Zeal
Yes columns are resizable. I have other columns in my datagrid which are resizable. But as soon i set AutoSizeMode=DataGridViewAutoSizeColumnMode.DisplayedCells to a column, this specific column isn't resizable anymore while others are.El
E
22

I finally find a way to do what I wanted.

The idea is to

  • let the dataGridView resize the columns itself to fit the content, and then
  • change theAutoSizeColumnMode and set the width with the value you just stored.

Here is the code:

dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
int widthCol = dataGridView.Columns[i].Width;
dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView.Columns[i].Width = widthCol;

Hope this will help.

El answered 19/7, 2013 at 13:45 Comment(1)
I have 17000 coulmsn i cant interate through the columns , it could take ahughe amount of timeChoreodrama
G
1

Building on the code above to iterate it for all columns and also auto adjust the width of the DataGridView

//initiate a counter
int totalWidth = 0;

//Auto Resize the columns to fit the data
foreach (DataGridViewColumn column in mydataGridView.Columns)
{
    mydataGridView.Columns[column.Index].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    int widthCol = mydataGridView.Columns[column.Index].Width;
    mydataGridView.Columns[column.Index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    mydataGridView.Columns[column.Index].Width = widthCol;
    totalWidth = totalWidth + widthCol;
}
//the selector on the left of the DataGridView is about 45 in width
mydataGridView.Width = totalWidth + 45; 
Gunar answered 5/4, 2016 at 11:18 Comment(0)
C
1

You can just use this simple code:

dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);

After setting datasource. It will set the width and allow resize.

Cota answered 19/1, 2018 at 11:4 Comment(2)
I think the point of OP's question, is the DGV will not allow manual column re-sizing when using the Displayed Cells resize mode. At least, it's not for me either - which led me here.Upsydaisy
Actually, I think this is the proper and best solution. It does what the OP has requested. @jwilo, this answer does not set the DGV.AutoSizeColumnsMode property. That could, indeed, prevent the manual column resizing. This answer calls a method that does one-time resize using the specified mode. The auto-size mode of the columns (or the grid) stays unchanged, and if it allows resizing, the columns remain resizable by the user. You can call it whenever you feel this auto-resize should take place.Interfertile
S
0

The thing that is not obvious (it was not to me) is that use of AutoSizeMode means we want the size to be managed, so that implies that the user would not (cannot) do a resize. So what I have tried and it seems to work is to use DataGridView.AutoResizeColumn (note that it does a one-time resize) for each column or most columns then set DataGridView.AllowUserToResizeColumns to true. There are probably other variations but the important thing is that use of AutoSizeMode is mutually exclusive with allowing the user to do a resize.

Simonides answered 7/12, 2016 at 23:47 Comment(0)
T
0
  • Thanks for the solution above (To iterate through the DataGridView.Columns, change AutoSizeMode to a valid one, collect width value and set it back after change AutoSizeMode to DataGridViewAutoSizeColumnMode.None).
  • I struggled with it, and noticed it won't work whenever it is called from the class constructor or any line before Form.Show() or Form.ShowDialog(). So I put this code snippet in the Form.Shown event and this works for me.
  • My transformed code, reguardless of whatever DataGridView.AutoSizeColumnsMode set before, I use DataGridViewColumn.GetPreferredWidth() instead of changing DataGridViewColumn.AutoSizeMode and set the width value immediately, then change DataGridView.AutoSizeColumnsMode once:

    private void form_Shown(object sender, EventArgs e)
    {
            foreach (DataGridViewColumn c in dataGridView.Columns)
                c.Width = c.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, true);
            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    }
    
  • Be sure to set

            dataGridView.AllowUserToResizeColumns = true;
    
  • I don't know how come this only works after the form is shown.

Truncate answered 13/4, 2017 at 14:38 Comment(0)
T
0

Go to the Design view of the form, select the desired dataGridView and set to Fill the property AutoSizeColumnMode under the "Layout" group.

Layout group

Talithatalk answered 22/6, 2024 at 20:55 Comment(0)
C
-1

Simple solution: Either bound filed or template filed. you can use ItemStyle-Width

<asp:BoundField DataField="SSSS" HeaderText="XXX"  ItemStyle-Width="125px"/>
<asp:TemplateField HeaderText="EEEE" ItemStyle-Width="100px">
Chub answered 5/4, 2019 at 14:41 Comment(1)
Can you please elaborate your answer and provide a concrete example? What is a "filed"? Should this read "field" or "file"? I'll upvote when done.Plimsoll

© 2022 - 2025 — McMap. All rights reserved.