is there a way to pass value/text/data from form to another form

I have a form contains StaffID and more and the form have a Search-button that when I press it it will open another form with a DataGridView then when I double-click a row it will pass the selected data to another form

the first form here First Form here

second form here Second form where data came from

so on the second form I will double click a row then it should send a data string to staffID textbox on the first form

here is my code (btw the to open the form2 is form2.ShowDialog())

my code to send data to the first form

private void dgw_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    FrmStaff frmStaff = new FrmStaff();

    if (e.RowIndex >= 0)
    {
        DataGridViewRow row = dgw.Rows[e.RowIndex];
        frmStaff.txtStaffID.Text = row.Cells[0].Value.ToString();
    }
}

the textbox is also modifiers : Public

but the problem is I don't get the value....

Upvotes: 2

Views: 170

Answers (2)

GingerNeil
GingerNeil

Reputation: 106

If you are wanting it to show the new form on the double click event as shown I would modify the FrmStaff to take the parameters into the constructors.

// In FrmStaff file
private string _staffId;

public FrmStaff(string staffId)
{
    _staffId = staffId;
}

Then when you do the

frmStaff.Show()

In the calling screen it will have the passed staffId internal to it now

Hope this helps?

Upvotes: 1

Tanmay Nehete
Tanmay Nehete

Reputation: 2196

There is another way to do it. By using the global variable class with static variables initialize this global class in the main method only.

code should be like shown below

public class Globalvaribale()
{
  public static string sVale="";
  public static string sVale1="";
}

In Main Method

 private void dgw_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        FrmStaff frmStaff = new FrmStaff();

        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = dgw.Rows[e.RowIndex];
            Globalvaribale.sValue= row.Cells[0].Value.ToString();
            string getdata = row.Cells[0].Value.ToString();
             Globalvaribale.sValue1 = "Sample";
            MessageBox.Show(getdata);
        }
    }

You can use the same variables with the same value complete application as well.

Upvotes: 1

Related Questions