Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am working with winform, I have a datagridview.. i have transferred the selected cell value to a new form like this

C#
private void editToolStripMenuItem_Click(object sender, EventArgs e)
    {

        Form2 f2 = new Form2();
            f2.label1.Text = dataGridView1.SelectedCells[0].Value.ToString();
            f2.ShowDialog();
    }

But now i want to transference the Textbox value which is in the form2 to that cell can i do that? means there is a textbox in the form2 which is textbox1 and a button i.e button1 when button is clicked then i want to transfer the textbox.text to the cell which was selected

I have a contextmenustrip in a menu named edit is written when edit is clicked it transfers the value to the label can i retransfer the value to that cell which i have selected

this is the second form button event where i have wirtten the code but when button is pressed it errors with index out of range.

C#
public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
       Records fs = new Records();
       textBox1.Text =  fs.dataGridView1.SelectedCells[0].Value.ToString();
        fs.Show();
    }
Posted

1 solution

Ok, if you want to transfer data back from Form2 to Form1 and put it back in the place that data came from, then unless you close Form2to return the data, then your only option is to use an Event.

This isn't difficult, but it takes a little work on your part.
You need an Event in Form2 which Form1 handles, and a Property in Form2 which returns the data.
Form2:
C#
/// <summary>
/// Event to indicate Data Available
/// </summary>
public event EventHandler DataAvailable;
/// <summary>
/// Called to signal to subscribers that aata is available
/// </summary>
/// <param name="e"></param>
protected virtual void OnDataAvailable(EventArgs e)
    {
    EventHandler eh = DataAvailable;
    if (eh != null)
        {
        eh(this, e);
        }
    }
void returnDataButton_Click(object sender, EventArgs e)
    {
    OnDataAvailable(null);
    }
public string Data
    {
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
    }
Form1:
C#
private void showForm2Button_Click(object sender, EventArgs e)
    {
    Form2 form2Instance = new Form2();
    form2Instance.DataAvailable += new EventHandler(form2Instance_DataAvailable);
    form2Instance.ShowDialog();
    }

void form2Instance_DataAvailable(object sender, EventArgs e)
    {
    Form2 f = sender as Form2;
    if (f != null)
        {
        textBox2.Text = f.Data;
        }
    }

What the code does is create an event in Form2, that tells Form1 that new data is available - the handler then picks it up from the property, via the instance of the form that reported data was available.

(Hopefully, by now you are comfortable with instances, events and as syntax so it all makes sense with a little reading)
 
Share this answer
 
Comments
shaikh-adil 6-Jan-13 6:25am    
thank you og sir
:)
for helping
OriginalGriff 6-Jan-13 6:43am    
You're welcome!
If you are going to be using events in the future (and you will, they are very, very useful) then have a look at this:
http://www.codeproject.com/Tips/400287/A-simple-code-snippet-to-add-an-event
It's a tip which makes it easier to add events to your code - follow the instructions, and then you can type "evh" in VS (like you type "prop" for a property template) and press TAB twice, and all you need to do is set the name and description, then signal it when you want by calling the "On..." method. Simples!

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