Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have two forms "Form1" and "Form2" in Form1 Datagridview is added and when i click on 1st cell at index[0,0] another form open namely "Form2" then in Form2 there was number of row in Datagridview now i want that when i click on any cell in Form2 Datagridview the value of that cell will be printed in the Form1 Datagridview.

Click hereto see image

Purchase pp = new Purchase();    
pp.ValGet(dataGridView1.Rows[0].Cells[0].Value.ToString());
this.Dispose(); 


above code is written in Form2 and this transfer the value of specific cell in ValGet() method present in Form1 now can you tell me how i set this value in Datagridview in form1
Posted
Updated 20-Mar-14 19:03pm
v3
Comments
ZurdoDev 20-Mar-14 14:25pm    
Show the code where you are opening the Form. Then just pass in the value. Where are you stuck?
Mufasil Muhammad Iqbal 20-Mar-14 14:34pm    
Please see below code.
Mufasil Muhammad Iqbal 20-Mar-14 14:44pm    
Updated the question.
Mufasil Muhammad Iqbal 20-Mar-14 14:28pm    
This is my Form1 code:
using System;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace Accounts
{
public partial class Purchase : Form
{

public Purchase()
{
InitializeComponent();
}
public void ValGet(String abc)
{
xDT.DataSource = abc;
}

private void xDT_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(0)) { ViewStock vs = new ViewStock(); vs.ShowDialog(); }

}


}
}

This is Form2 Code:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace Accounts
{
public partial class ViewStock : Form
{
SqlConnection xConnection;
//SqlCommand xCommand;
DataTable xTable;
SqlDataAdapter xSDAdaptor;
public String value;
DataTable xTable2;
public ViewStock()
{
InitializeComponent();
xConnection = new SqlConnection("Data Source =.; DataBase=AccDB; UID=sa; Password=123;");
}

private void ViewStock_Load(object sender, EventArgs e)
{
xTable = new DataTable();
xSDAdaptor = new SqlDataAdapter("select * from stock",xConnection);
xConnection.Open();
xSDAdaptor.Fill(xTable);
xSDAdaptor.Dispose();
xConnection.Close();
dataGridView1.DataSource = xTable;
}



private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Purchase pp = new Purchase();
pp.ValGet(dataGridView1.Rows[0].Cells[0].Value.ToString());
this.Dispose();
}
}
}
ZurdoDev 20-Mar-14 14:34pm    
Please put this code in the original question, by using the Improve question link. And please be clear about where you are stuck. Just dumping in a lot of code and expecting us to debug it is not very useful.

Hey just suggesting(as RyanDev said, use a reference),
1.put a constructor in Form2 to accept a object of Form1(declare another Form1 object as public in class level, and assign passed object to that inside the constructor),
2.when you create Form2, use that constructor and pass along Form2 object(ie: "this"),
and open Form2 as a dialog.
3.And, in the CellDoubleClick event, use public Form2 object to access it's contents(set the required cell value in FOrm1).
and you are done.
*I suggest not to declare a new object of Form1 in the CellDoubleClick event, because it creates a new instance, not the one you had.

public Purchase purchase = new Purchase();

C#
//1.
public ViewStock(Purchase pp) //this is a new constructor
{
     InitializeComponent();
     xConnection = new SqlConnection("Data Source =.; DataBase=AccDB; UID=sa;                                                                                       Password=123;");
     purchase = pp; 
}

//2.
private void xDT_CellEnter(object sender, DataGridViewCellEventArgs e)
{
     if (e.ColumnIndex.Equals(0)) { ViewStock vs = new ViewStock(this); vs.ShowDialog(); }            
}

//3.
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
     //Purchase pp = new Purchase();
     //no need to use above object, since we have the global object "purchase" 

     //use "purchase" object to set your selected value to wherever you want.
     this.Dispose();   
}
 
Share this answer
 
v3
Comments
Mufasil Muhammad Iqbal 21-Mar-14 8:18am    
Thanks milindaSnow your solution help me alot thanks
milindaSnow 23-Mar-14 23:38pm    
my pleasure mate.. if you can give a rating..
cheers..
Giving ChildForm a reference to ParentForm, as proposed earlier, is a way to strongly coupling the two forms. ChildForm has to have knowledge about the inner workings of ParentForm to update it correctly.

This is bad.

I'm for a similar solution, a little simpler to implement and the knowledge reversed. This is also a little better since child forms tend to be simpler than parent forms. Therefore ParentForm has to have less knowledge of Childform as would be the other way round.

See this:
C#
class ParentForm : Form
{
    void GetDataFromChild()
    {
        ChildForm child = new ChildForm();
        DialogResult dialogResult = child.ShowDialog();

        if(dialogResult == DialogResult.Ok)
        {
            pp.ValGet(child.Value);
        }

        child.Dispose();
    }
}

class ChildForm : Form
{
    internal float Value {get; private set;}

    void CellClick(object sender, EventArgs e)
    {
        Value = GetCellValue(e);
        this.DialogResult = DialogResult.Ok;
    }
}
In WinForms, to set the DialogResult property of a form also causes closing it. So control is given back to ParentForm, which acts according to DialogResult. Value can be read after the closing of ChildForm until its disposal.
 
Share this answer
 
v2

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