c# pass parameters from a previous action - c#

I'm writing an application that will have a datagridview control and 4 textboxes.
One requirement for the application is that when the user double clicks a row in the datagrid, the 4 textboxes fill with the corresponding textboxes with the data -> this is already done and working. Once the user has edited the field and clicks OK, they will be saved on the datagridview.
On the other hand, if the user clicks a button called "Add Register", the same buttons will be enabled and once they click OK a new register with the data in the textboxes will be created.
The thing is that I don't know how and I have not found any information about knowing if I should edit a row (and which one) or add a new one when OK is clicked.
In other words: how can I know if I'm coming from an "edit" request or an "Add register" request when the user presses "OK"?
Thanks!

Welcome to StackOverflow.
The simplest way to achieve this is to have a boolean field on your form, e.g. bool isEdit. When the user double clicks a line in the DataGridView (a handler for which you say you have working), then set the field to true. Within the Add register button handler, set the field to false.
Now within your OK handler, you simply examine the value of the isEdit field to know, if you are adding or editing!
I have done a lot (a very great lot!) of such DataGridView forms. Personally I do not like to combine the form with data entry. I prefer to leave my DataGridView form as totally read-only, and then when the user clicks edit or add buttons, I display a separate modal form to handle the data entry. On saving from the modal form, I refresh the grid.
HTH

Related

ASP.NET - Can't save changes on GridView because of button Postback

I have a GridView, and buttons that do actions (Edit, Save, etc). The GridView is created dynamically. Once I press Edit button, I add textbox (controls) on every cell of the GridView, with their respective text value, so it can be edited.
The problem comes when I click Save button, because when the method starts, it seems like there isn't any textbox in the gridview... so that makes me think if it's because some autopostback stuff when the Save button it's clicked.
Any thoughts about solving that?
EDIT: Clarifying what I want:
1) I got a gridview with data,
2) I put the data of each cell from each row into textboxes,
3) I put those textboxes in the gridview,
4) I do change data in those textboxes,
5) I can't save that data because when I click the Save button, the data on the textboxes deletes itself
You can't avoid postbacks, ASP.NET works with them.
When you click the save button, the page does postback and the Load event fires. In that event, you have to rebuild the page as before you clicked Save (put the textboxes).
If you don't do it, the textboxes aren't there no more and no text can be saved.
Edit: gives unique names to your textboxes, it seems non-sense, but ASP.NET need to hooks on names

Stateful Winform and Cancel Buttons

With the prevelence of the "OK" and "Cancel" buttons at the bottom of forms/dialogs, it is odd to me that I can't seem to find a "standard" way to save control state.
For instance, I have a checked list box of filters. When the user clicks the OK button, it applies the filters to a data set and the form closes. If the user clicks the cancel button, the form undoes all the checked-item changes and the form closes.
In a perfect world, when the user clicks the "OK" button, the saved control state is overwritten with the current control state and a new-state flag is set. When the form is closing, if the new-state flag is set the form resets the flag, and if it is not set the form replaces the displayed control with the saved control state. That way if the cancel button is hit, all the checked-changes the user made are reset.
What is the best-practice way of handling a cancel button undoing changes to a control, or even an entire form? Is there a best practice solution? I could see this being necessary for text boxes, radial buttons, check boxes, and practically every control, so please try and keep it generic and not specific to checked list boxes.
I would suggest it's as simple as:
Keep the data reflected in the UI separate from the UI itself
When the form is loaded, set its contents based on the data
When the user clicks OK, save the changes to the data model (however that is achieved, which will depend on exactly how you're populating the model)
When the user clicks Cancel, don't save any changes
There's no need to "undo" the changes on Cancel - you just throw away the form. When you next want to show the form, the same data will be loaded as before, because you didn't save any changes to it.
The easy way: don't re-use form instances. Do var childForm = new MyChildForm(); before each childForm.Show();

Web application Listview question

In a list view in C# how can I make all the fields in a column editable at once? Right now I have the standard edit button which allows me to update just one line at a time. I want the end user to be able to click a button and allow then to edit the column in its entirety.
Rebind the ListView with all TextBoxes when the user clicks edit.
You would have to manually track if you're in edit mode or not and on postback loop through all the rows retrieving the values from the textboxes and store them.
There is no built-in functionallity for what you're trying to do but it's possible.

swicth combobox to textbox

I have add a options form to my application. There is 2 radio buttons and when the options form is closing that form saves the data to database user is selected witch one of these radio buttons.
And I have another form is writing some data to database, user is selecting a item from combobox's list to add. I want to user can add that data manually too without of this combobox. When user select "manual" on options form, combobox must disappear and a textbox must appear and user is selected "list" then combobox must appear.
Hot can i do that? I need a new sql query too.
If it's just the two controls, one easy way of doing it would be to place both of them on the form, on top of each other, and then just make one of them invisible, and when the user selects the other options, just change visibility on both of them.
You'll have to make sure that your form closing code that saves the data reads from the correct control of course, but that's just the matter of a simple if statement to check which of them is visible.

C# - Refresh dropdown after a child form closes

I have a c# application with multiple "worker" forms. These forms have numerous comboboxes that are populated from the database on form load, with 'add' buttons beside them. When a user clicks the add button the administrative form is opened allowing the user to add a corresponding value to the database.
For instance, the combobox may be a list of street types. "Drive" is not in the street types table in the database, so the user wants to add it. They click the add button and the admin form is loaded so they can add the "Drive" value to the street types. When the admin form closes, I want to repopulate the combobox upon return to the worker form.
Any insight as to the best way to accomplish this?
Thanks guys. I used ShowDialog and it worked great.
Administration adminForm = new Administration();
adminForm.tcAdministration.SelectedIndex = 1;
adminForm.ExistingCaseNumber = this.ExistingCaseNumber;
adminForm.ShowDialog();
this.PopulateComboBoxes();
When the forms that can administer the lists is created, I'd add an event handler for the Closed event of the form. In that event handler is where I would reload the data source for the list, and then rebind it to the combobox.
One way is to create the form as a modal form, and you can use the this.Parent, and access a public method from there that updates the combo box.
It takes more work to set it up if you haven't from the start, but if you're doing the proper MVP[~] thing, that child "Add" form should trigger an update in your model, which your controller observes, which reacts by updating that portion of the view.
[~] Martin Fowler has retired his use of the term Model-View-Presenter, but he's still waffling between Supervising Controller and Supervising Presenter as its replacement.
A couple references:
http://martinfowler.com/eaaDev/SupervisingPresenter.html
http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
You can also do:
Form1 frm = (Form1)Application.OpenForms["Form1"]; This will allow you to update the form from another one.

Categories