Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to transfer record from datagridview of form to another form in c#?

like This;
i have two form:
form1 (textboxs and button for datagridview form)
form2 (only Datagridview for select row)

when i run the app, and press the button for the search of datgridview (inside the form1) display the form2(datagridview).then i select one row and assign these record to the textbox of form1.

Anyone Can Help Me ...
Posted
Comments
Itz.Irshad 30-Oct-12 5:10am    
What I perceive from above description is that, you need to get values in Main Form (Form1) TextBox(s) controls on the Selection of row in a DataGridView in Sub-Form(Form2). If it is like that, then you just need to write some code in Selection_Changed event of Sub-Form(Form2) DataGridView and Assign the Selected Row Cells Value to TextBox Controls in MainForm. (If you need help in code then please mention your code, so I can figure out what is missing).

If form2 known in from1:

C#
public partial class Form1 : Form
{
    Form2 form2;
    public Form1()
    {
        InitializeComponent();

        dataGridView1.Rows.Add(new object[] { "A1", "A2" });
        dataGridView1.Rows.Add(new object[] { "B1", "B2" });
        dataGridView1.Rows.Add(new object[] { "C1", "C2" });
    }

    private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
    {
        if (null != dataGridView1.CurrentRow)
            form2.ShowDataGridViewRow(dataGridView1.CurrentRow);
    }

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


C#
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    internal void ShowDataGridViewRow(DataGridViewRow dataGridViewRow)
    {
        label1.Text = dataGridViewRow.Cells[0].Value.ToString();
        label2.Text = dataGridViewRow.Cells[1].Value.ToString();
    }
}


the rest should be obvious ...
 
Share this answer
 
v2
Comments
Zain -Ul- Arifeen 30-Oct-12 12:43pm    
plz,plz,plz,plz,plz sir write the complete code .or put statement in my code.Dn't mind it,because i am beginner..
Zain -Ul- Arifeen 30-Oct-12 12:46pm    
plz, plz, sir ,can you explain me this in the form picture with code. plz, plz, plz, plz, plz,.....
Zain -Ul- Arifeen 31-Oct-12 9:49am    
thank you so much
BlackMilan 1-Nov-12 3:31am    
So if your problem is solved, them accept a solution ...
You need to write that code in dataGridView1_SelectionChanged Event instead of dataGridView1_CellClick Event. And use this object
FrmUsers fpc = new FrmUsers();
instead of creating new object in event handler.
 
Share this answer
 
Its Very Simple Your method will not work because you make object of form1 each object has its own memory...So it will defiantly not going to be work so for that..
Make one static variable name Id (If u have id in table 'login')
or you can use whatever field you may like to use instead of id
In form1...
C#
public static Int32 UserID;

private void btnsearch_Click(object sender, EventArgs e) 
{ 
Form2 f2 = new Form2(); 
fr2.ShowDialog();

if (UserID != 0)
{
//Fill The TextBox Using The UserID
....
//Set UserID again to 0
               
UserID= 0;

}

}


in Form2 at click event..
C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
//just set UserID because it is staic we can easily give it value

Form1.UserID = 12;//Give your value for UserID

this.close();
}



Or Use can make more static variables each for user-password,user-role and so on...but i will not suggest that method...Use Above method it is easiest way...Gud Luck
 
Share this answer
 
Please see my article written specially for such cases: 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