i have 2 forms one of them is my main form where my datagridview is,and the second form where my textbox is ,and i need to make any input on the textbox shows in my datagridview but it didnot appear
here is my form1 code
namespace datatabl
{
public partial class Form1 : Form
{
DataTable customers =new DataTable();
public Form1()
{
InitializeComponent();
create();
dataGridView1.DataSource = customers;
}
private void create()
{
customers.Columns.Add("FirstName", typeof(string));
customers.Columns.Add("LastName", typeof(string));
}
public void addrows(string firstname,string lastname)
{
customers.Rows.Add(firstname, lastname);
}
private void newToolStripButton_Click(object sender, EventArgs e)
{
Form2 d = new Form2();
d.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
and my second form code is
namespace datatabl
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.addrows(textBox1.Text, textBox2.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
Loading
VulpesPosted Jul 18, 2012, 9:20 AM
Form1 f = new Form1();
to this:
Form1 f = (Form1)Application.OpenForms["Form1"];
addrows should be left as a public instance method.
amrPosted Jul 18, 2012, 9:26 AM
amrPosted Jul 18, 2012, 9:16 AM
Posted Jul 18, 2012, 7:27 AM
private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.addrows(textBox1.Text, textBox2.Text);
}
Here you're creating a new instance of the Form1 and you're adding the rows. It will create a completely new object including your grid, customer datatable etc.
So, there is no relation between your old instance of your Form1 and new instance of Form2.
Currently I don't have vs to post code the correct. But what you cab try is, change the method signature from public addrows to static and try it out.
If you have any questions, please let me know.