Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new to c# programming and hope to get a simple awnser here() only if posible :)
i am trying to get to following in my program

form1:

textbox1 ("value")
textbox2 ("value")
these are public

into form2 ("Datagridview1") Product[0]and price[1]

but if i use

FORM1:
form2.dataGridView1.Rows[0].Cells[0].Value = textbox1.Text;
this.dispose();

it's not fillng my datagridview
while form2 is up and running it's only working if i
do it like this

form2 form2 = new form2();
form2.dataGridView1.Rows[0].Cells[0].Value = textbox1.Text;
form2.Show()

but i need it to something like a live update of the datagridview from form 1
can someone help me please?

it need to be this way instead of the other way around
Posted
Updated 21-Aug-14 11:29am
v2

There are many valid ways to go about making inter-Form communication possible. A key question in this type of scenario is "what implements what ?"

If your Form1 creates, and 'Shows, Form2, then the "live" instance of Form1 can keep a reference to the "live" instance of Form2.

So, if you define a public method in Form2, code in Form1 can access it via the reference to Form2.

For example:
C#
// in Form2 where there is a DataGridView Control named 'dataGridView1

public void updateDataGrid(string updateText)
{
    dataGridView1.Rows[0].Cells[0].Value = updateText;
}
The advantage of a public method in Form2 is the fact that you have kept the two Forms relatively "loosely coupled;" the instance of Form2 doesn't know "who" calls this method. Trust me, that's good.

In your case, it's important to define what you mean by "live update." Is it the case that every time the user interacts in any way with the TextBox on Form1 you want that change instantly updated in the DataGridView Cell on Form2 ?

In that case, consider defining a TextChanged EventHandler for the TextBox on Form1, and call the public method on Form2 that you have a reference to, passing the current 'Text property of the TextBox.

If Form2 is not created by Form1, then a different strategy is indicated.
 
Share this answer
 
v2
Comments
pim krook 22-Aug-14 1:03am    
first thanks for your reaction
but is like
form1 gets the information of my product database
and form 2 has to add it to the grid at the moment i click on addbutton in form 1
so they don't overwrite but just add a new line everytime
BillWoodruff 22-Aug-14 5:36am    
Then just call the public method in Form2 from the code in Form1 when the user presses the 'Add button in Form1. In the public method in Form2's code "do the right thing" to add the new information to the appropriate place in the grid.

Be sure and define the reference you keep to the instance of Form2 you create in Form1 at Form1 scope level, so you can re-use it: in other words define it outside the 'Load event of Form1. Example:

// in Form1
Form2 currentForm2;

private void Form1_Load(object sender, System.EventArgs e)
{
   currentForm2 = new Form2();
   currentForm2.Show();
}
Try this its very simple. Lets consider Form1, Form2 both contains textboxes as textBox1. Add following code in Form2.

C#
public void GetTriggeringControl(TextBox textBox)
{
    if (textBox != null)
    {
        textBox.TextChanged += new EventHandler(textBox_TextChanged);
    }
}

private void textBox_TextChanged(object sender, EventArgs e)
{
    textBox1.Text = ((TextBox)sender).Text;
}

Lets consider Form1 has button named as button1 on its click event add following code.
C#
private void button1_Click(object sender, EventArgs e)
{
    Form2 obj = new Form2();
    obj.GetTriggeringControl(textBox1);
    obj.Show();
}

Change you code as required.
 
Share this answer
 
v2
Comments
pim krook 22-Aug-14 1:18am    
hi also thanks for the reaction but
i my case both of the forms are already open. and as you are showing in the button_click event
you are opening form2 again this has to be not the case. it's like click on form1 addbutton and on that time is should appear in the datagridview on form 2 while both of the forms are open
am i right or am i seeing this wrong ?
i am opening form 1 in form2 like

ob form1 = new ob();
form1.Show();

then i need to get the information from textbox1 on the moment i click on the addbutton
like
pfc form2 = new pfc();
form2.dataGridView1.Rows[0].Cells[0].Value = naamTextBox.Text;
this.Dispose();
okay you want both forms to be open an information be transfered within them? if that is the case then do this, here is a detail example:
:
C#
using System;
using System.Drawing;
using System.Windows.Forms

namespace Liveexchage
{
public partial Form1 : Form
{
// declare form2
private Form2  form2transfer;
}
public Form1()
{
InitializeComponent();
}
public void SetForm2(Form2 frm)
{
form2transfer = frm;
}
private void btnTransfer_Click(object sender, EventArgs e)
{
form2transfer.dataGridView1.Rows[0].Cells[0].Value
= textbox1.Text;
}


and that's all, but remember to set the modifier of the necessary controls to public.
Regards
 
Share this answer
 
Comments
Ese Ochuko 23-Aug-14 4:07am    
please note that just a rough code, so you should correct the opening and the closing brace respectively
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
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