How to fetch values from one form to another?
am having two forms in my project in form2 created datagridview its havng the memcode and memname in form1 having two textbox wen am click on the textbox1 it shud go and fetch the f memcode in form2 at the same time shud display the memname also in textbox2 in form1... please help me how can i do....????
theLizardPosted Jul 10, 2012, 3:41 AM
To add the component to the tool box, right click on TooBox, click Chose Items, click Browse.
Goto the folder where you put the project files, click bin, debug, finally chose form2formaccess.dll
If you do this right, the component should be in your tool box items.
backiyalakshmi KPosted Jul 10, 2012, 3:11 AM
theLizardPosted Jul 10, 2012, 2:48 AM
Do you get this error while in the IDE?
Did you add the component to your tool box?
backiyalakshmi KPosted Jul 10, 2012, 2:40 AM
theLizardPosted Jul 9, 2012, 6:41 PM
While the methods used by others will be trivial to very complex, the object is, is to get or set property values on other forms that do not necessarily give access to their properties.
To make this job as simple as possible, I decided to make a component that is dropped on a form, this component acts as an agent that gets and sets property values.
The component Form2FormAccess.dll is free to use and is included in the project files for download.
In your example you have two forms, one with 2 text boxes to receive the cell values of the data grid view on form 2, with the forms agent this is how it is done.
Form1 code
//--------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int row=0;
public Form1()
{
InitializeComponent();
//Create Form2 and show it;
formAccess1.SendToForm = new Form2();
formAccess1.SendToForm.Show();
}
private void textBox1_Click(object sender, EventArgs e)
{
//access form2 variable row
row = ((Form2)formAccess1.SendToForm).row;
//get the value of cell 0 in selected row
textBox1.Text = ((DataGridView)(formAccess1.Ctrl(formAccess1.SendToForm, "dataGridView1"))).Rows[row].Cells[0].Value.ToString();
}
private void textBox2_Click(object sender, EventArgs e)
{
//access form2 variable row
row = ((Form2)formAccess1.SendToForm).row;
//get the value of cell 0 in selected row
textBox2.Text = ((DataGridView)(formAccess1.Ctrl(formAccess1.SendToForm, "dataGridView1"))).Rows[row].Cells[1].Value.ToString();
}
}
}
//--------------------------------------------------------------------------------------------------
Form2 Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public int row = 0;
public Form2()
{
InitializeComponent();
DataGridViewRow row = new DataGridViewRow();
DataGridViewCell[] c = new DataGridViewCell[2];
c[0] = new DataGridViewTextBoxCell();
c[1] = new DataGridViewTextBoxCell();
row.Cells.AddRange(c);
dataGridView1.Rows.Add(row);
dataGridView1.Rows[0].Cells[0].Value = "3563653467";
dataGridView1.Rows[0].Cells[1].Value = "Jack Armstrong";
dataGridView1.Rows[1].Cells[0].Value = "99678666";
dataGridView1.Rows[1].Cells[1].Value = "Bugs Bunny";
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
row = e.RowIndex;
}
}
}
This Tree View was filled by the forms agent, it has found all child controls of an MDI application, this is the only code that would need to be placed on your form to acheive the same result.
formAccess1.GetControlTree();
formAccess1.ControlTree.Parent = pnTreeView;
formAccess1.ControlTree.Dock = DockStyle.Fill;
formAccess1.ControlTree.AfterSelect += tv_AfterSelect;
If you want to use the component in your ide, it must be installed into your tool box.
Enjoy.
VulpesPosted Jul 9, 2012, 10:37 AM
backiyalakshmi KPosted Jul 9, 2012, 7:44 AM
VulpesPosted Jul 9, 2012, 7:28 AM
1. The name of the control is not dataGridView1 - the spelling needs to be exact.
2. The DGV has not been placed on Form2 but within some container such as a Panel or GroupBox.
backiyalakshmi KPosted Jul 9, 2012, 7:13 AM
VulpesPosted Jul 9, 2012, 5:32 AM
1. The DGV on Form2 has been placed directly on that form and is called dataGridView1.
2. memcode and memname are displayed in row 0, columns 0 and 1 of dataGridView1.
Then the code to do that from Form1 would be:
private void textBox1_Click(object sender, System.EventArgs e)
{
Form2 f2 = (Form2)Application.OpenForms["Form2"];
DataGridView dgv = (DataGridView)f2.Controls["dataGridView1"];
textBox1.Text = dgv.Rows[0].Cells[0].Value.ToString();
textBox2.Text = dgv.Rows[0].Cells[1].Value.ToString();
}