|
-
Nov 21st, 2021, 05:44 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] DataGridView Cell value when using Timer to trigger an event.
When entering text into a datagridview I am using a timer that ticks after the user stops typing.
The issue is the cell's value it is reporting as the old value. It's not being 'validated'. Not sure if validated is the right term.
Is there a way to kick the contents of the TextBox into the cell after the timer ticks?
-
Nov 21st, 2021, 05:48 PM
#2
Thread Starter
Fanatic Member
Re: DataGridView Cell value when using Timer to trigger an event.
After quite some time of messing around, and a minute after I hit post, I realised I just needed to add DataGridView.EndEdit.
Maybe it was writing the problem down that made me think.
Last edited by sgrya1; Nov 21st, 2021 at 06:51 PM.
-
Nov 22nd, 2021, 04:06 AM
#3
Re: [RESOLVED] DataGridView Cell value when using Timer to trigger an event.
You probably ought to give a bit more thought to whether that is actually the correct solution. It may be, but it may not be.
The way a DataGridView is designed, it contains no controls by default. What you see in the cells is simply a rendering of a control. If each cell actually contained a control then the whole thing would be extremely sluggish. When you start editing a cell, a control will be created if required (if the last cell edited was the same type, the same control will be used) and embedded in the cell. If the cell is in a DataGridViewTextBoxColumn then the control will be basically a standard TextBox. The Value property of the cell is assigned to the Text property of the editing control and away you go. That's why the grid doesn't raise any keyboard events or the like while you're editing - it's actually the editing control that receives keyboard entry and the like. When an editing session completes successfully, the Text property of the editing control is assigned to the Value property of the cell and the editing control is removed.
By calling EndEdit, you are causing the data to be pushed from the editing control to the cell, which you wanted, but also for the editing control to be removed. What if the user had just paused typing and wants to type more? What if the user changes their mind and wants to press Escape, which calls CancelEdit and removes the editing control without pushing the data to the cell?
Maybe what you actually should be doing is getting the intermediate data from the editing control, rather than the cell. Also, even if you are going to use a Timer to call EndEdit, that's not where you should get the cell value. You probably ought to be handling the CellEndEdit event of the grid and stopping the Timer there, so it won't Tick if the user ends the editing session themselves. You should also be handling the CellValueChanged event, which will be raised when the Value property of a cell changes, which it will if the user changes the cell contents and then the editing session is ended in any way other than cancelling it.
-
Nov 22nd, 2021, 05:24 AM
#4
Thread Starter
Fanatic Member
Re: [RESOLVED] DataGridView Cell value when using Timer to trigger an event.
jmcilhinney,
This is extremely helpful.
It perfectly explains what EditingControlShowing is (something that I added but didn't understand), and the confusing behaviour I've struggled with.
And you are exactly right about EndEdit not being the perfect thing. I would like the option if continuing typing. Thank you so much for taking the time to explain this. My understanding of what I am working with has greatly improved.
Can you help with another oddity?
When another control has focus, then a datagridview row header is clicked (which highlights the row), the focus remains (or returns) to the previous control.
The only way I can delete a row is if I first enter a datagridview cell then select the row header.
Edit : This is probably obvious from the behaviour I describe but I do have "SelectionMode = FullRowSelect" and "EditMode = EditOnKeystrokeOrF2"
Last edited by sgrya1; Nov 22nd, 2021 at 06:03 AM.
-
Nov 22nd, 2021, 07:25 AM
#5
Re: [RESOLVED] DataGridView Cell value when using Timer to trigger an event.
 Originally Posted by sgrya1
Can you help with another oddity?
When another control has focus, then a datagridview row header is clicked (which highlights the row), the focus remains (or returns) to the previous control.
The only way I can delete a row is if I first enter a datagridview cell then select the row header.
Edit : This is probably obvious from the behaviour I describe but I do have "SelectionMode = FullRowSelect" and "EditMode = EditOnKeystrokeOrF2"
You've marked this thread Resoloved and this is a different issue anyway, so you should start a new thread specifically for this new question.
-
Nov 22nd, 2021, 08:11 AM
#6
Thread Starter
Fanatic Member
Re: [RESOLVED] DataGridView Cell value when using Timer to trigger an event.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|