Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

I have DataGridView in my winform application and set

this.dgvDte.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;

When run my project an open that form some times but not always get me this err

"System.InvalidOperationException: This operation cannot be performed while an auto-filled column is being resized"

any body can help me what its happen ?

like image 748
Aftab Garm Avatar asked Feb 18 '13 10:02

Aftab Garm


3 Answers

this.dataGridView1.ColumnHeadersHeightSizeMode is set to AutoSize.

While creating DataGridView, if the mouse is suddenly on the position where the Column header would place, DataGridView would try to handle the CellEnter event, and try to resize the columns or something, but right now the DataGridView is still creating.

As a result, a InvalidOperation is thrown.

Workaround is as following:

In customer provided project, GridWrapper.cs file, put the following line after InitializeComponent();

this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

Put the following line in GridWrapper_Load method:

this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;

Many customers have found it useful to discuss issues like this in the forums where Microsoft and other members of the community can recommended ways to achieve the behavior you are aiming for.

Answer in the link

like image 84
DenisShvatskiy Avatar answered Nov 11 '22 01:11

DenisShvatskiy


As reported on the MS forums (by MS), the exception is thrown by the DataGridView. As far as I can tell this occurs when the mouse cursor is inside the datagridview clientrectangle when the control is initialized. MS tells us they allow ("by design") that the column resize events and cell enter events fire at the same time forceing the Exception to be thrown. The workaround suggested did not fix my problem.

Since MS seems not to be interested in fixing this problem, I have found, for me, two solutions work:

  1. make sure the new form opens in a "safe" position, just out of the way of the mouse cursor
  2. move the mousecursor to a save position while the constructor of the form runs

You can get and set the current mouse screen position through 'System.Windows.Forms.Cursor.Position'.

Although moving the mouse cursor without user input is not always appreciated, if done consistently in an application, it is the most acceptable option for me.

This is the code I tend to use in the constructor of the form containing a DataGridView (after the InitializeComponent method)

            Cursor.Position = this.PointToScreen(new Point(this.Width/2, -10));

This puts the cursor centered on the form in the title bar. You may have to do some checking to make sure you do not move the cursor off-screen.

I hope this may help you a bit.

like image 40
Leo Prast Avatar answered Nov 11 '22 02:11

Leo Prast


Maybe you are iterating on the values while they are being loaded? Try to get your application to wait or lock on those operations.

like image 1
David S. Avatar answered Nov 11 '22 03:11

David S.