Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have window form1 and when i click getData it open new window form2, inside form2 i have datagridview, when i click the cell i want to fill the textbox of the form1 textbox fields.

What I have tried:

C++
Note : i make checkInfrm fields public in visual studio property 

// checkInfrm 

    public partial class checkInfrm : Form
    {
        public checkInfrm()
        {
            InitializeComponent();
        }

        private void btnListGuest_Click(object sender, EventArgs e)
        {
            Forms.listGuestFrm listGuest = new Forms.listGuestFrm();
            listGuest.ShowDialog();
        }
...
   }



```


// listGuestFrm 
public partial class listGuestFrm : Form
    {
        public listGuestFrm()
        {
            InitializeComponent();
        }

       private void dgvGuestList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
               
                if (e.RowIndex != -1)
                {
                   
                    DataGridViewRow row = dgvGuestList.Rows[e.RowIndex];
                    frmGuest guest = new frmGuest();
                    checkInfrm checkIn = new checkInfrm();
                    Int32 guestId = Convert.ToInt32(row.Cells["id"].Value);


                        this.Close();
                        checkIn.txtId.Text = row.Cells["id"].Value.ToString();
                        checkIn.txtGuestId.Text = "G" +row.Cells["id"].Value.ToString();
                        checkIn.txtGuestName.Text = 
                        row.Cells["guestName"].Value.ToString();
                        checkIn.txtGender.Text = row.Cells["gender"].Value.ToString();
                        checkIn.txtContactNo.Text = row.Cells["countactNo"].Value.ToString();
                        checkIn.txtOccupation.Text = 
                         row.Cells["occupication"].Value.ToString();
                        checkIn.txtIdType.Text = row.Cells["idType"].Value.ToString();
                        checkIn.txtIdNo.Text = row.Cells["idNo"].Value.ToString();
                        Retrieve.loadText("sp_fillGuestBalance", 
                         checkIn.txtBalanceValue, guestId);
                       
                        checkIn.Show(); // it is working, but it open me new window
                        //checkIn.Items.Add(checkIn);
                        //checkIn.fillValues();

                    }
      }
}
Posted
Updated 17-Jan-21 3:45am
v7

This can be solved in several ways, a simple approach is using static variables, see:
How to Pass Data One Form to Another in Windows Form Application[^]

Some other options are discussed here: Passing Data Between Forms[^]

Or you could do it like this:
public partial class FormMain : Form
{
	FormSub form2;

	public FormMain()
	{
		InitializeComponent();
	}

	private void button1_Click(object sender, EventArgs e)
	{
		form2 = new FormSub();
		form2.TextBoxValue = "First Value";
		form2.Show();
	}

	private void button2_Click(object sender, EventArgs e)
	{
		form2.TextBoxValue = "Changed Value";
	}
}

public partial class FormSub : Form
{
    public FormSub()
    {
        InitializeComponent();
    }

    public string TextBoxValue
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
}
 
Share this answer
 
v3
Comments
Member 14921707 16-Jan-21 10:43am    
all the above article i already read them,, pass data for new opening window.
my question how can i pass data if there is already open window, instead of opening again
RickZeeland 16-Jan-21 10:48am    
Take a look at the last example "The Delegates Approach".
Member 14921707 17-Jan-21 3:12am    
i am asking simple question. i have already open window (checkIn widow)..i want to fill my guest information in to it, when i click list of guest it open window with grid view, when i click the cell i want to fill the first window guest information. how can i do this?? ``` checkInfrm.Show()``` open me new window i dont want this
``` update my code please!!```
RickZeeland 17-Jan-21 3:27am    
Take a look at button2_Click() in the last example, it does not open a new form.
And remember: I do not have your complete code with all the forms and datagridview!
Member 14921707 17-Jan-21 3:48am    
see my update code..i add full code
Here is another example that shows how you can use a Static form to pass the value from the sub form to the main form (a better way to do these kind of things would be by using events btw)
static class Program
{
    public static FormMain formMain;

    static void Main()
    {
        formMain = new FormMain();
        Application.Run(formMain);
    }
}

public partial class FormMain : Form
{
    FormSub form2;

    public FormMain()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        form2 = new FormSub();
        form2.Show();
    }

    /// <summary>
    /// Used to set the main textbox text from the sub form
    /// </summary>
    public void SetMainTextBoxValue(string text)
    {
        this.textBox1.Text = text;
    }
}

public partial class FormSub : Form
{
    public FormSub()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Set the main textbox text
    /// </summary>
    private void button1_Click(object sender, System.EventArgs e)
    {
        Program.formMain.SetMainTextBoxValue(this.textBox1.Text);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900