how to pass the values between the datagridview's in different pages
-
Hello, In my application i am having two forms. In the first form i am having a textbox and in the second form i am having a datagridview and a button back. when the button back is clicked i like to get the data from the datagridview to the textbox in the first form. Please help me how i can do this. I have no sound about this....plz help me Thanks in advance
-
Hello, In my application i am having two forms. In the first form i am having a textbox and in the second form i am having a datagridview and a button back. when the button back is clicked i like to get the data from the datagridview to the textbox in the first form. Please help me how i can do this. I have no sound about this....plz help me Thanks in advance
Just showing example :
public class Form1 : Form
{
...
public string MyText
{
set { this.textBox1.Text = value; }
}
...
}public class Form2 : Form
{
private Form1 form1;public Form2(Form1 form1)
{
this.form1 = form1;
}private void buttonBack_Click(object sender, EventArgs e)
{
this.form1.MyText = "Data from DataGridView here...";
}
} -
Just showing example :
public class Form1 : Form
{
...
public string MyText
{
set { this.textBox1.Text = value; }
}
...
}public class Form2 : Form
{
private Form1 form1;public Form2(Form1 form1)
{
this.form1 = form1;
}private void buttonBack_Click(object sender, EventArgs e)
{
this.form1.MyText = "Data from DataGridView here...";
}
}This is NOT a good way to do it as now Form2 is coupled to Form1. Also, what if there are hundreds of objects that need the data? Are you going to have hundreds of constructor overloads? The correct way to do this is to raise an event in Form2 that Form1 (and any other interested parties) subscribes to. If any data needs to be passed, then a custom class derived from event args should be used with that data being passed as a property. This is the way all the classes provided by Microsoft do it, with good reason. A quick example:
using System;
using System.Windows.Forms;public class Form1 : Form
{
public Form1() { }public void ShowNewForm2() { Form2 form2 = new Form2(); form2.CustomEvent += new EventHandler<CustomEventArgs>(form2\_CustomEvent); form2.Show(); } void form2\_CustomEvent(object sender, CustomEventArgs e) { // data is in: e.YourData }
}
public class Form2 : Form
{
public event EventHandler<CustomEventArgs> CustomEvent;public Form2() { } protected virtual void OnCustomEvent(CustomEventArgs e) { EventHandler<CustomEventArgs> eh = CustomEvent; if (eh != null) eh(this, e); } public void PerformCustomEvent() { OnCustomEvent(new CustomEventArgs(123)); }
}
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(int yourData)
{
YourData = yourData;
}public int YourData { get; private set; }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hello, In my application i am having two forms. In the first form i am having a textbox and in the second form i am having a datagridview and a button back. when the button back is clicked i like to get the data from the datagridview to the textbox in the first form. Please help me how i can do this. I have no sound about this....plz help me Thanks in advance
See my reply to stancrm - there is an example there that gives you the idea. :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
See my reply to stancrm - there is an example there that gives you the idea. :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Thank you for giving me a nice example. :)
-
Just showing example :
public class Form1 : Form
{
...
public string MyText
{
set { this.textBox1.Text = value; }
}
...
}public class Form2 : Form
{
private Form1 form1;public Form2(Form1 form1)
{
this.form1 = form1;
}private void buttonBack_Click(object sender, EventArgs e)
{
this.form1.MyText = "Data from DataGridView here...";
}
}thank you for giving me an idea. i got it :)