Passing Parameter to a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 2 forms
Form1 has a datagrid with read-only data.

Form 2 is for editing/inserting/deleting the data on Form 1. It shows many
of the same fields that are on Form 1, but via a different Proc and dataset
than on Form 1 (so I think this excludes the currency manager). This
arrangement is also good, as I want to limit which fields are able to be
edited.

I want to be able to pass a parameter (key field value) from the selected
row on Form1 that will be the parameter used for the Select Proc on Form 2.

I am having trouble knowing how to initialize and open Form 2 with the
parameter value from Form 1.

Specifically, I need to know where to put these lines of code, so that the
"strContractNumber" value will be used to initialize the form correctly.

prm = SqlSelectCommand1.Parameters.Add("@ContractNumber", SqlDbType.VarChar,
50)
prm.Value = strContractNumber

Any help appreciated
Nick
 
Hi Nick,

Add a public property to the form2 called i.e. ContractNumber

private string m_strContractNumber;
public string ContractNumber
{
get { return m_strContractNumber; }
set { m_strContractNumber = value; }
}

then, when u want to show form2, initialize this property before showing the
form:

....
using (Form2 form = new Form2())
{
form.ContractNumber = "1234"; // assign it to selected row contract
number value
form.ShowModal();
}
....

Then when user presses 'search' button (or what ever you've got there :)
use m_strContractNumber to set the value of the parameter

Hope this helps
 
Many THanks Milosz, that worked nicely. I am new to dot net, but beginning to
enjoy it.
 
Back
Top