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 ?
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
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:
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.
Maybe you are iterating on the values while they are being loaded? Try to get your application to wait or lock on those operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With