How to save the value of a cell of a DataGridView before edition for undo purpose? - vb.net

I am adding an undo feature for a DataGridView using the Command Design Pattern.
To create the EditCommand, I have to know what was the previous value of the cell, so I can re-enter it in case the user hits undo.
I'm doing it using the CellBeginEdit and CellEndEdit events.
With the CellBeginEdit, I save the value of the cell in a private variable and at the CellEndEdit function, I use the saved value to create the EditCommand.
It's working fine, but I wonder if there is a more saner way to do it. Maybe a event that holds together the previous and the new cell value.
Bonus points if I could actually prevent the DataGridView from updating itself and let the EditCommand do it.

I recently implemented Undo / Redo in a large WPF application, and was able to extract the Undo / Redo logic into a stand-alone library. This is available on CodePlex.
The library helps keep a stack of history that can be re-applied if the user elects to undo / redo. This could replace your private variables and also allow for multiple items in the undo history.
You can find documentation and downloads at http://muf.codeplex.com/

Related

MS access event load form

I have about 20 MS-Access databases with about 400 Forms and wish to perform an action whenever any form is loaded.
I would need, perhaps, an event at database or application level that would trigger at any form being opened/loaded. Then I might need only to add code once to each of the 20 databases, but not to each of the 400 forms.
And: it has to be in VBA (Access >= 2010)!
My Question: Is this even possible? And if so, does someone have a hint?
Thanks,
Pete
There are multiple ways to go about this, I think.
What I'd probably do is the following:
Create a hidden form that opens whenever your database opens (using an autoexec macro) with a timer.
When loading the hidden form, initialize a collection/multi-level array of all available forms in the database, and set their status to closed (somewhat difficult, probably would go with multilevel array or a collection inside a collection to be able to store form name + boolean open or closed).
Periodically iterate the collection, check for changes, so you can see whenever a form closes and trigger your event.
You could also use VBA to iterate through all the forms, add a module to them if there is none, and then add your desired Form_Load code to that module. (Would probably be wise to simultaneously create code to undo that action, so you can actually maintain the code). While refining that, you could check if the form already has a Form_Load action, and append code to that if it exists.
Alltogether, possible, but difficult. If you want pointers on some of the steps, I can give them, but for major issues on the implementation, I'd ask a separate question.

Create a shared copy and paste menu for my grids

I have 20 or so grids in my application suite. I'd like to create a global copy/paste context menu which I can bind to every single grid rather than code in each form.
I am unsure what is the best way to achieve this, I have started to create a class with my menu in it, but get stuck at the point of adding the actual menu options. For example I know I'll need to call a "copy" event, but I also know I'll need to tell it what I am copying, and I cannot see how that is done in vb.net when you can only add the address of a method minus parameters.
e.g.
.MenuItems.Add("Copy Cell", New System.EventHandler(AddressOf CopyCell))
Obviously I want "CopyCell" to only be coded in one place as well, rather than repeated in each form. I will always be copying the same object (SelectedCellCollection).
I am not sure how to make the menu have an event with parameters, or how to make it "know" that I want to always copy the selected items. I'm aware that I'd have to do some coding in the form but just trying to work out the way to minimize it.
I have created my own context menu class (via inheritance) with specific copy and paste functionality / options tailored to the grid I am using. It works fine and only needs one line of code per form/grid to activate.

Detecting if user has made changes to FontDialog object

I've got a FontDialog box called aFontDialog.
Can I detect changes made to this dialog box?
Initially my object creates the dialog using this code aFontDialog.ShowDialog, the user than makes changes, then if the user is happy with their changes then the application will receive Windows.Forms.DialogResult.OK:
Is it possible to detect any changes made to this dialog by the user? Will I need to record the state of the different aspects of the dialog before and then compare to how they are after - or are there some properties or methods built into this dialog box that help me find any changes?
The most important concern here is - why do you need to know the changes. See, font is usually not a transactional object, so you normally don't need to avoid excessive network traffic or minimize number of database roundtrips.
I would just look if user pressed OK. If yes, set the new font, regardless of how similar it is to your current one. It's just one line of code - simple as assigning this new font to the old one:
Me.Font = MyFontDialog.Font 'Me could be any control in this case
Besides, I think it is your only way, if the font is different. Meaning you cannot for example set Font.Bold = True, because it's read-only. And it would not take a lot of processing time either, so no point in optimizing it.
If you really want to, you can examine FontDialog.Font after checking DialogResult for OK, and compare to what you passed there, although I don't see where this would be useful.

Is there an event which fires before a user enters a row into the datagridview in .net 3.5

I am trying to create a wrapper to replace the sheridan datagrid actvex control with a datagridview.
The sheridan datagrid control supports an event which fires just before the user is going to add a row to the grid called BeforeInsert.
Does anyone know of an equivalent event in the datagridview control or or some other code I could write which produces the same effect?
I'm having a look at all the events here, I don't see one for Adding or Inserting a row, just Added. Although there's one for Deleting and Deleted strangely enough. I won't pretend to know why. To your question, how exactly will users be adding rows? You could try playing around with the "AllowUserToAddRowsChanged" event and change that property programatically when the user wants to add a row.

ASP.NET Keep fileupload after postback

I'm writing an intranet ASP.NET page using VB.NET. I've run into a particularly nasty problem dealing with handling file uploads. I'll do my best to explain the problem, and perhaps someone can help.
My problem is almost a duplicate of this one, or this one, except (other than the filename) I don't care about sending the file to the server until the other data has been reviewed.
Here's the situation:
Joe Q. Dataentry inputs some data into several fields. The first 3 are drop down, and when he changes the selection, a postback event is fired that queries a database for valid entries for the other drop down selections. After selecting the values, he inputs some other data, chooses a file to accompany the data and clicks the "Update" button. When he hits the button, it fires a postback event that sends the current data to the server to be validated. The data will create a change in the database, so he is presented with a view of the current state, and what it will look like when his changes are made. He can now either confirm or cancel the operation for whatever reason.
Part of the data he will see involves the extension of the file which may be a PDF, or could also be some image file or other document.
Now here's where my problem is - on each postback event, the fileupload dialog is cleared. I was getting around it by creating a temporary file on the first postback and then renaming if he clicks OK or deleting on Cancel... but I need to do a variety of things, based on the previous state of data and the filename. I've tried to keep some session variables to retain the filename, and that works OK for just renaming the file, but for what I need to do it gets unwieldy.
What I want to do is be able to have the postback event to present the changes, and then when the user clicks "OK", submit the file. Is there any possible way to do that?
One of my thoughts was to do some of the validation client-side (I'm already re-validating server side so I'm not too worried about data security there), but I don't know how I could get the information from the database query.
Thanks for any help, and reading my slightly convoluted story/situation!
EDIT:
It appears that what I want to do is prevent a certain button from firing a full postback. Is there any way to do that?
EDIT II:
I have an update panel on the page already - is there any way for the button to only post what's in the update panel?
What you might want to do is place your drop-downs inside of an ASP.NET AJAX UpdatePanel, and keep your file upload control out of that.
Your update panel will do the post backs and allow your validation logic to happen without submitting the file, then when you hit your final "Save" button (which is also outside of your UpdatePanel) the entire form will be submitted back and you can work with your file then.