when I want to store/pass a value, I always use the .Tag property. for example, when I store the value:
Form prosesEdit = new FormProsesChemicalRawWbEdit();
prosesEdit.Tag = (int)this.proses_chemicalDataGridView.Rows[e.RowIndex].Cells[1].Value;
prosesEdit.ShowDialog();
Then, I would pass the value into my EDIT form like this:
proses_chemical_id = (int) this.Tag;
this.proses_chemicalTableAdapter.FillByChemId(this.mcd_softwareDataSet.proses_chemical, proses_chemical_id);
this.proses_chemical_listTableAdapter.FillByChemId(this.mcd_softwareDataSet.proses_chemical_list, proses_chemical_id);
but recently, I was supposed to store & pass 2 different values. let’s say (int)this.proses_chemicalDataGridView1.Rows[e.RowIndex].Cells[1].Value; and (int)this.proses_chemicalDataGridView2.Rows[e.RowIndex].Cells[1].Value;
how exactly do I do that?
Thanks
Both of the answers given so far will work but I want to elaborate on the answers a little and provide an alternative. Let’s say you create your own class like this:
Then you can assign an instance of your class to the Tag property like this:
And you can get the instance of the class back out of the Tag property like this:
However, it’s generally bad practice to use the Tag property to pass values around in forms because it requires a lot of type casting and knowledge of what is stored in each tag.
A more robust way is to use constructor injection since you know the type of form you are creating:
You would then most likely have to store those values as properties or fields inside the form. The reason this is a more robust approach is that it’s resistant to accidental change. In other words, if you need to pass in a 3rd value in future it’s less likely you’ll forget to change code that needs changing.
You could also use property injection here but I’m not sure of your requirements so I’ll leave it to you to decide.