title: How can I pass data to DataGridView from another form?
body: <p>I just want to pass data to DataGridView from another form?</p>
  
  <p>I have 2 windows forms:  </p>
  
  <ul>
  <li><p><code>form1</code> contains <code>DataGridView1</code> and <code>button_frm1</code>. The <code>DataGridView</code> have 3 columns and already have some data (6 rows) and <code>DataGridView1</code> modifiers = Public.</p></li>
  <li><p><code>form2</code> contains <code>textBox1</code> and <code>button_frm2</code>.</p></li>
  </ul>
  
  <p>Now, when I click <code>button_frm1</code> form2 appears and next when I click <code>button_frm2</code> the value in the textBox should be inserted into <code>DataGridView1</code> in column0 in the selected row. But instead, I got this error:</p>
  
  <blockquote>
    <p>Index was out of range. Must be non-negative and less than the size of the collection.</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Here is the code I tried:</p>
  
  <p>Form1:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void button_frm1_Click(object sender, EventArgs e)
      {
          Form2 frm2 = new Form2();
          frm2.Show();
      }
  
  
  }
  </code></pre>
  
  <p>Form2:</p>
  
  <pre><code>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();
  
  
      }
  }
  </code></pre>