How to get cell value change when closing form? - c#

I have a form with a DataGridView on it.
In this DataGridView there is a DataGridViewComboBoxColumn. When changing the value of a cell of this DataGridViewComboBoxColumn, the CellValueChanged-event is only fired when leaving the cell (for example, if I click into another cell).
The event is not fired when I change the value and then just close the form.
So, how can I save the changes (if there are any changes), if my form is simply closed?
[UPDATE]
The CellValueChanged is not fired when the form the DataGridView is on is shown through form.ShowDialog():
using (FormWithDataGridView form = new FormWithDataGridView()) {
form.ShowDialog(); // DataGridView on form fires no CellValueChanged-Event when form gets closed
}

From the community content post on the MSDN entry for ShowDialog, when you close a modal form, it is just being hidden so the calling code can still have access to the DialogResult or other properties of the form. Apparently, this is why the CellValueChanged event on the DataGridView is not firing (CellParsing and CellEndEdit events also do not get raised).
As a workaround, in the form closing event, you can remove focus from the DataGridView, which will cause the CellValueChanged event to fire if necessary. If you don't have any other controls on the form to set focus to, you can put a label on the form and give it focus.

have you tried using dataGridView1.CellParsing? This should (as I understand it) trigger even if the focus of the cell is lost due to closing the containing Form. This will only trigger if the user has changed the value of the cell.
[Edit] Now when I think about it a bit more I would try dataGridView1.CellEndEdit if I was you. That is better..[/Edit]

Ran into a similar situation myself, but with the last checkbox updated programmatically--whouldn't update unless I switched rows.
Try calling the BindingSource.EndEdit method just before you update the combobox.
For me I call the data adapter afterwards and it recognizes a change to the datagridview--so it does the work.

Related

Textbox leave event unless tab changes

Using c# winforms vs2008
I'm using a TabControl on a form. Each tab has a different set of input fields. Some of those fields has to be completed. Such as Username. Multiple users will use the same PC, so a the username must remain static. I have a leave event on the require fields which triggers when the textbox loses focus to check if a value was added. In the case of Username, a popup would then presents possible values. That works pretty awesome to ensure accuracy and speed. To problem comes in when the user clicks on another tab, the textbox leave event triggers. How can I prevent the leave_event code from running if the focus changes to a control not on the current Tab?
I have tried different event handlers, but the leave event seem to occur first. I tried textbox validating event handler, but also no success. I tried adding a If-statement in front of the code to check the tab number, or tabcontrol state - no joy - as before anything else changes, the leave event fires.
The only remaining thing I can think of is to loop through all the controls on the tab to check if they have focus, but that just feels messy.
Any other suggestions?
I have found a partial solution. Using a Mouse_Enter and _Leave event on the tab, I set a flag to determine whether the mouse was clicked in the form or outside. If the flag is set to false, the leave event dont run. This will not solve short cut key presses though. But that I can handle later.

TextChange in Gridview Textbox prevent button event

I have a text box on a Gridview which I'm allowing some data to be edited. When the enter key is hit, the TextChanged event happens, like I'd expect. But when i change the data and click a button, the button effect not happen. I must click the button again to fire button event. I think it happen because the grid didnot lost it focus.
How to make it possible fire the button event just in one click?
You can change your gridview element's IsTabStop property to false to gain that effect.
Also if you want to cycle with Tab you can TabNavigation="Cycle" .
Both changes are in xaml.
Not much we can do to help you here without the code itself but here are couple things you could try to debug this:
Try different browsers – it may be a browser issue
Try some client side debugging – just open console and see if anything happens when you click the button for the first time

Validating a DataGridView in C#

I have a parent form with one DataGridView and a button. When the button is clicked, it opens a child window and when closed, goes back to the parent form. I want to validate the value being inputted in a cell of the DAtaGridView. I'm using CellValidated event and showing a message box whenever it inputs an invalid value. However, when I click on the button to open the child window and close it back, the DataGridView becomes just a white box with a diagonal cross, and a NullReferenceException shows.
Does anyone know what causes this issue? If not, what's the best way to validate a value of the cell and which event to put it in?
Just setup Visual Studio as described in this article: How to: Break When an Exception is Thrown. This way, you will be able to catch this exception in the debugger, and see where its roots are.
Whenever validating a cell in a DataGridView, you should use the CellValidating event.
You can get the data using the DataGridViewCellValidatingEventArgs and set the Cancel property to True if it is invalid data. This is gonna prevent the CellValidated event from being raised and should give the focus back to the given cell.
If this doesn't help, can you provide some code and show us where the exception occured?

In Winforms, why is validation not being fired after leaving TextBox and entering a DataGridView?

I'm overriding the OnValidating event in a custom Winforms text box. I'm finding that if the text box (which is bound to an object) has focus and then I give a grid focus using the mouse, the OnValidating event doesn't always get fired. When I first give the grid focus, it gets fired fine. But, if put one of the grid's cell in edit (blinking cursor), from there on out it seems to not get fired when I go back between the text box and grid using the mouse. If I change focus using the tab key, the validating always gets fired. If I give focus to a non-grid control using the mouse, the validation is always getting fired.
I tried to recreate this functionality from scratch in a simple form and I can't recreate the problem. The grid I'm using in the setup where I'm getting the problem is a custom DataGridView with custom column types. I'm wondering if the grid is the problem. But, I don't see how it could affect the text box events. Any ideas?
It probably has to do with the CausesValidation property.
A control's validation is suppressed if focus is going to a control that has CausesValidation set to false. It's just a wild guess, but I'm thinking some control inside the grid has CausesValidation = false;
This property is meant for things like "Cancel" buttons, but can cause lots of confusion.

How do I disable a button cell in a WinForms DataGrid?

I have a WinForms application with a DataGridView control and a column of DataGridViewButtonCell cells within that. When I click on one of these buttons, it starts a background task, and I'd like to disable the buttons until that task completes.
I can disable the DataGridView control, but it gives no visual indication that the buttons are disabled. I want the user to see that the buttons are disabled, and to notice that the task has finished when the buttons are enabled again.
Bonus points for a method that allows me to disable the buttons individually, so I can leave one of the buttons enabled while the task runs. (Note that I can't actually give out bonus points.)
Here's the best solution I've found so far. This MSDN article gives the source code for a cell class that adds an Enabled property.
It works reasonably well, but there are two gotchas:
You have to invalidate the grid after setting the Enabled property on any cells. It shows that in the sample code, but I missed it.
It's only a visual change, setting the Enabled property doesn't actually enable or disable the button. The user can still click on it. I could check the enabled property before executing the click event, but it also seemed to be messing up the appearance when the user clicked on it. Instead, I just disabled the entire grid. That works alright for me, but I'd prefer a method that allows me to disable some buttons without disabling the entire grid.
There's a similar sample in the DataGridView FAQ.
You could give this a try:
When you click on the cell...
Check to see if the process with the current row identifier is running from a class-level list; if so, exit the cell click event.
Store the row identifier in the class-level list of running processes.
Change the button text to "Running..." or something appropriate.
Attach a basic RunWorkerCompleted event handler to your process (explained shortly).
Call backgroundWorker.RunWorkerAsync(rowIdentifier).
In the DoWork event handler...
Set e.Result = e.Argument (or create an object that will return both the argument and your desired result)
In the RunWorkerCompleted event hanlder...
Remove the row identifier from the running processes list (e.Result is the identifier).
Change the button text from "Running..." to "Ready"

Categories

Resources