Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi all,

I have two DataGridView docked in my Form in which I am setting its AutoSizeMode to Fill I want to change automatically the second DataGridView column Width the same as the first one's column width change.

I tried these two cases:

private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
    dataGridView2.Columns[e.Column.Index].FillWeight = e.Column.FillWeight;
}


private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
    dataGridView2.Columns[e.Column.Index].Width = e.Column.Width;
}


In the two cases this error appear when loading the form that contains these two DataGridView :

This operation cannot be performed while an auto-filled column is being resized.

Please can anyone helps me ?

Thank you in advance,
:)
Posted
Updated 1-Dec-16 5:02am
v5

The error message tells you why this fails. You have the AutoSizeMode set to Fill and cannot therefore set the width manually.

You will need to change the AutoSizeMode to either NotSet or None on the grid that you want to set the size manually on.
 
Share this answer
 
v2
Comments
Michael Waguih 19-Feb-11 6:57am    
When I do this The second DataGridView never change its size and each column have the size of the width set to each column forever
Henry Minute 19-Feb-11 7:26am    
I have just made a test project with two datagrids the first has the AutoSizeColumnsMode set to fill the second is set to none. I have used the second of your code snippets. The DGVs have their Anchor set to Left|Top|Right so that they will follow the width of the form. When I run it and adjust the Form's width, the columns in grid 2 adjust to match those in grid 1.

I can only suggest that in your case grid 1 column sizes are set before the event handler gets hooked up. As an experiment try setting your Anchor as described above and run your app then resize the Form. If grid 2 does adjust to match then I am correct.
Michael Waguih 19-Feb-11 9:23am    
I have already docked my dataGridViews did it differs from beeing anchored
Henry Minute 19-Feb-11 9:26am    
So long as they resize as the Form resizes, no different.
try this code

C#
foreach (DataGridViewColumn dcol in dataGridView1.Columns)
            {
                dcol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
 
Share this answer
 
Comments
[no name] 5-Jul-12 6:21am    
the last post was on 19 Feb '11. It's already more than a year.
C#
dataGridView1.AutoResizeColumns();
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
 
Share this answer
 
Comments
#realJSOP 1-Dec-16 12:19pm    
Dude. This question is almost 6 years old. Pay attention.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900