1
votes

I just want to pass data to DataGridView from another form?

I have 2 windows forms:

  • form1 contains DataGridView1 and button_frm1. The DataGridView have 3 columns and already have some data (6 rows) and DataGridView1 modifiers = Public.

  • form2 contains textBox1 and button_frm2.

Now, when I click button_frm1 form2 appears and next when I click button_frm2 the value in the textBox should be inserted into DataGridView1 in column0 in the selected row. But instead, I got this error:

Index was out of range. Must be non-negative and less than the size of the collection.

Please help me how to insert the textBox value from form2 into DataGridView1 in form1. What steps to follow? Thank you very much in advance.

Here is the code I tried:

Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

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


}

Form2:

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

    private void button_frm2(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();
       textBox1.Text= frm1.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();


    }
}
1
how do fill your DataGridView? - StepUp
@naouf Have you tried my approach? Do I understand you correctly? - StepUp
Hi StepUp. thank you for your reply. I still didn't try it because I am new to c# and your code is something new to me so I am still trying to understand it. However once I try it I will let you know. can you please tell in what area in c# should I search to understand your code? thank you. - naouf

1 Answers

1
votes

At first create a class which contains data about an event :

public class ValueEventArgs : EventArgs
{
    private string _smth;
    public ValueEventArgs(string smth)
    {
        this._smth = smth;
    }  
    public string Someth_property
    {
        get { return _smth; }
    }     
}

Then declare an event and event handler at the Form2:

public delegate void FieldUpdateHandler(object sender, ValueEventArgs e);
public event FieldUpdateHandler FieldUpdate;

and at the event handler of event "Click" of the button of the Form2:

private void button_frm2(object sender, EventArgs e)
{
    //Transfer data from Form2 to Form1
    string dataToTransfer=textBox1.Text;
    ValueEventArgs args = new ValueEventArgs(str);
    FieldUpdate(this, args);
    this.Close();
}

then write where you are calling the form2 from form1:

private void button_frm1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.FieldUpdate += new AddStuff.FieldUpdateHandler(af_FieldUpdate);
    frm2.Show();
}

void af_FieldUpdate(object sender, ValueEventArgs e)
{
   DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
   row.Cells[0].Value = e.Someth_property;
   row.Cells[1].Value = "YourValue";
   /*or
   this.dataGridView1.Rows.Add("1", "2", "three");
   this.dataGridView1.Rows.Insert(0, "one", "two", "three");
    */
   dataGridView1.Rows.Add(row);
}