There are two forms. In the first list of customers, the second form to edit the record. After editing, the client data is updated in the database and the first form is updated for the client. The question is how to correctly implement this task? To transfer the data from the first form to the second form, and after editing, to transfer back the changed data? At the moment I am passing through properties, but I don’t know how correct it is. Can you please give a general outline? further himself.
- is it important for you to know whether the object has changed or not? - RusArt
- and what a database? data is filled in via DataGridView? - Clarence
- Competently transferring data is possible only through the database, which serves as the only repository of all changes. In order for the changes on the first form to be visible after correcting the data with the second, the second form must do UpDate () first. (By mistake, I made this remark as an answer, and I already had time to write a comment to it :) to which I will answer below. - Alexander Muksimov
- The comment was as follows: "... Oh, do you advise. Why should the database know that I clicked the" Edit record "button? Let only the edited object get. - Ruslan Artamonov 4 minutes ago ..." - dear Ruslan talking about the change in the database, and therefore you should not produce entities and have a "hemorrhoid" with the introduction of synchronous changes in them - Alexander Muksimov
- @Alexander Muksimov I correctly understood that you advise getting an object from the database at the beginning of editing, saving it to the database, then requesting from the database again after editing? - RusArt
|
2 answers
The code for the first form I suggest something like this:
//редактируемый объект Data data; EditorForm editor = new EditorForm(); //внедряем редактируемый объект в форму редактирования editor.Data = data //EditorForm при нажатии на кнопку OK делает валидацию и редактирует объект из полей //Соответственно, если нажали OK - значит объект изменился. //Обратно ничего передавать не надо if (editor.ShowDialog() == DialogResult.OK) { //сохраняем объект в БД //Например, для Entity Framework context.SaveChanges(); } |
From the first form, transfer the data, say, to an object / structure, in the constructor of the second form, and in the class of the second form, select the public method for which the first form pulls after editing is completed.
something like this:
using (ВашаВтораяФорма f = new ВашаВтораяФорма(ВашОбъектСДанными)) { if (f.ShowDialog() != DialogResult.OK) { return; } var ИзмененныеДанные = f.GetData(); // действия с ними - обновление первой формы, запись в БД и т.д. } - Or get the object through a public property. And it is possible not to receive it at all if we pass a reference in the constructor and form 2 changes the object by reference. - RusArt
- And what is the principle of working out the public method? I do not understand the meaning of "jerks". - Alexander Puzanov
- "Dernet" - will save in the database and transfer to the first form ??? - Alexander Puzanov
|