how to pass data in DatagridView from Form1 to Form2 ?

faysal131313

New member
Joined
Sep 1, 2019
Messages
3
Programming Experience
Beginner
how to pass data in DatagridView from Form1 to Form2

when I click in Form 1 on search button to show me form 2
and when i selected row or more in DateGridView(in Form2)
it must add in form1(in DataGridView) ....but nothing add or change in form1 in DataGradview
 
Hard to identify a problem when you're not showing us what you tried.

Depending how many items are in your dgv, you could create a class object of values to bounce around between forms.
 
this is code
first i make Form1 and it contain DGV1 and button search
and i make dgv Modifier =public
i want take variable from Form2 (DGV2)to Form1(DGV1)

Form1:
namespace dgv
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

Form2:
namespace dgv
{
    public partial class Form2 : Form
    {
        DataTable dt = new DataTable();
        
        public Form2()
        {
            InitializeComponent();

            db.Open();
            db.GetData_DGV("select * from items ", dt);
            dgv2.DataSource = dt;
          
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            
            foreach (var dr in this.dgv2.SelectedRows)
            {
                var drv = dr as DataGridViewRow;
                frm.dgv1.Rows.Add(((DataRowView)drv.DataBoundItem).Row.ItemArray);
            }
        }
    }
 
Last edited by a moderator:
I recommend going back to whatever C# tutorial you were using and review the concept of instances.

Presumably, your Program.cs instantiates a Form1 object and runs it. When the user clicks on the "Search" button which is presumably your button1 button, then in Form1.button1_Click(), you instantiate a new Form2 object. When you run the code for that instance in Form2.button1_Click(), you instantiate a brand new Form1 object. It is not the same instance that you had originally running at beginning in Program.cs.
 
In Form2, you have code that creates a new Form1 instance and does something to it. How can that help you affect the Form1 instance that already exists? If you had a notepad and I went and bought a new notepad of the same type and wrote on it, would you expect what I wrote to magically appear in your notepad? Of course not, so why should that happen with forms? The proper way to do this is for Form2 to simply expose the data and then for Form1 to retrieve it.

I don't know what's in the tutorial recommended above but you might also follow the Blog link in my signature below and check out my posts on Data Among Multiple Forms. You can skip the first part because that is specific to VB. Part 2 is the quick and dirty way and part 3 is the right way.

If you want to close the second form when the data is available then you should almost certainly be calling ShowDialog rather than Show. In that case, you can simply retrieve the data when the method returns. If Show is the right option because you want the second form to remain open then the second form should raise an event that the first form can handle in order to know when to retrieve the data. In fact, the data can even be provided by the event if you like. There's a post on Custom Events in my blog too.
 
In Form2, you have code that creates a new Form1 instance and does something to it. How can that help you affect the Form1 instance that already exists? If you had a notepad and I went and bought a new notepad of the same type and wrote on it, would you expect what I wrote to magically appear in your notepad? Of course not, so why should that happen with forms? The proper way to do this is for Form2 to simply expose the data and then for Form1 to retrieve it.

I don't know what's in the tutorial recommended above but you might also follow the Blog link in my signature below and check out my posts on Data Among Multiple Forms. You can skip the first part because that is specific to VB. Part 2 is the quick and dirty way and part 3 is the right way.

If you want to close the second form when the data is available then you should almost certainly be calling ShowDialog rather than Show. In that case, you can simply retrieve the data when the method returns. If Show is the right option because you want the second form to remain open then the second form should raise an event that the first form can handle in order to know when to retrieve the data. In fact, the data can even be provided by the event if you like. There's a post on Custom Events in my blog too.

thanks for reply but i am sorry for my issue i programer in vb and i try to learn c# by professional learn
but this code work in vb and path data from form1 to form 2
 
It looks like your were just scanning for code to copy and paste from the blog post instead of actually reading the accompanying text. Keep reading his blog post. It comes in 3 parts. As he said, the first 2 parts are in VB.NET, but part 3 is in C# and VB.NET. The correct approach is in part 3.

Or you could look at Curtis's tutorial which is all in C#, but again, you should be reading to understand the concepts and understand why things are done this way otherwise you would just be doing cargo cult programming.
 
As he said, the first 2 parts are in VB.NET, but part 3 is in C# and VB.NET. The correct approach is in part 3.
Actually, parts 2 and 3 provide both VB and C# examples. It's only part 1 that is VB specific because it uses default form instances.
 
thanks for reply but i am sorry for my issue i programer in vb and i try to learn c# by professional learn
but this code work in vb and path data from form1 to form 2
It's no wonder you're having trouble learning on your own if you can be given specific instructions and simply ignore them. If you had looked at parts 2 and 3 of my blog post series as I instructed then you'd have seen that there were C# examples provided. I suspect that there is a language issue here but if you're going to ask a question in English then you need to be able to at understand at least the basics of an answer provided in English.
 
I personally decided this issue

saving data to disk and reading from disk
 
Last edited by a moderator:
But that's the brute force solution for inter-process communications (e.g. communicating between two different processes). The problem the OP is trying to solve is for intra-process communication (e.g. communicating within the same process). Saving data to a file and reading it back in within the same process is like a parent in the kitchen sending a text message to their teenager in the adjacent living room to tell them that dinner is ready. Sure, it works, but it's also bordering on the absurd.
 
Back
Top Bottom