Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, I got 2 forms

form1 is got 1 button 1 datagridview what I want to do when I click button open the form2 as show dialog

here is the codes for form2

private void btnImport_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.ShowDialog();
}

after form2 is opened I got 2 button 1 listbox in the form
what I want to do when I click the first button import my email address into listbox and when I click the second button I want to do move listbox1 items to form1 datagridview but I'm getting not seeing my email address in datagridview

here is the codes

private void btnImport_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Multiselect = false;

    DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.OK)
    {
        System.IO.StreamReader sr = new System.IO.StreamReader(@ofd.FileName);
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            listBox1.Items.Add(line);
        }
    }
}


private void btnMove_Click(object sender, EventArgs e)
{
    Form1 frm1 = new Form1();
    foreach (var item in listBox1.Items)
    {
        int idx = frm1.dataGridView1.Rows.Add();
        frm1.dataGridView1.Rows[idx].Cells["Email"].Value = item;
        this.Close();
    }

}


when I use the all codes at 1 form its work perfectly but when I want to seperate to 2 forms I'm not getting anything

how can I move listbox items to datagridview from form2 to from1

What I have tried:

Form1 frm1 = new Form1();
foreach (var item in listBox1.Items)
{
    int idx = frm1.dataGridView1.Rows.Add();
    frm1.dataGridView1.Rows[idx].Cells["Email"].Value = item;
    frm1.Show();
    this.Close();
}


this work but its opening another form1 so I don't want this I want to get back same form

Form1 frm1 = new Form1();
foreach (var item in listBox1.Items)
{
    int idx = frm1.dataGridView1.Rows.Add();
    frm1.dataGridView1.Rows[idx].Cells["Email"].Value = item;
    frm1.SendToBack();
    this.Close();
}


this sending back to me same form but I'm not seeing the listbox items in datagridview

private void btnMove_Click(object sender, EventArgs e)
{
    Form1 frm1 = new Form1();
    foreach (var item in listBox1.Items)
    {
        int idx = frm1.dataGridView1.Rows.Add();
        frm1.dataGridView1.Rows[idx].Cells["Email"].Value = item;
        this.Close();
    }

}


also this sending back to me same form but I'm not seeing the listbox items in datagridview
Posted
Updated 18-May-22 5:54am

You create new instances of Form1 so there is no data in it to be moved. You need to use the original instance created at the beginning of the program. A better option is to add a method in Form2 that accepts strings which it adds to its ListBox. You then call that method from Form1 passing all the relevant data.

For further information take a look at this excellent article (and the following two) written by our own OriginalGriff: Transferring information between two forms, Part 1: Parent to Child[^].
 
Share this answer
 
Comments
YusufA0 18-May-22 11:30am    
let me check it
YusufA0 18-May-22 11:44am    
I checked it I'm sorry I'm still newbie at c# I'm confused
I found the solution I created 2 static and 1 object , one static & object at form1 one static at form2

public static Form1 instance;
public DataGridView dgv1;
public Form1()
{
    InitializeComponent();
    instance = this;
    dgv1 = dataGridView1;
}


public static Form2 instance;
public Form2()
{
    InitializeComponent();
    instance = this;
}


and when I want to transfer data I just did this

foreach (var item in listBox1.Items)
{
    int idx = Form1.instance.dgv1.Rows.Add();
    Form1.instance.dgv1.Rows[idx].Cells["Email"].Value = item;
    this.Close();
}
 
Share this answer
 
Comments
OriginalGriff 18-May-22 12:07pm    
This is a very bad solution: it breaks OOP principles, and will really mess up should you ever need more than one instance of any form (and that happens, quite a lot). Go back to what I replied, and follow the link. It shows exactly what to do, and even includes code to do it!
YusufA0 18-May-22 12:09pm    
okay sir since I'm newbie I'm confused when I checeked it but I'll check it again and will update the solution thanks
OriginalGriff 18-May-22 12:39pm    
Avoid public and / or static variables: if you think you need one, your design is generally wrong! :laugh:
PIEBALDconsult 19-May-22 15:37pm    
Please don't try to answer your own question. Use the Improve Question button to add detail to the question.
YusufA0 20-May-22 3:25am    
oh okay I'm newbie at this platform next time I'll do that

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