0

I want to access a DataGridView from another form. I just want to get a value from DataGridView and show it in a textbox located in another form.

As shown in the photo: I click Edit line button and Form2 pops up, so when i click button1, textbox1 should have the value of row[0] cell[0] (which is "1") but instead i get this Error: An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll.

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

Is there anything I am missing? I made DataGridView Modifiers = Public but still didn't work. please help thank you.

Photo

naouf
  • 627
  • 2
  • 18
  • 28
  • You should take a look at delegates: http://stackoverflow.com/questions/2019402/when-why-to-use-delegates – Nataniel Richardt Nov 04 '15 at 18:01
  • Please paste your code, not a screenshot of your code. –  Nov 04 '15 at 18:02
  • Never access `SelectedRows` without checking if there are any selected rows! Also your new form1 is just that: __new__, not the other one! Eric shows you one common approach to pass a reference out. Instead of the DGV you could just as well __pass out the form1 itself!__! – TaW Nov 04 '15 at 18:06
  • Possible duplicate of [Add a datagridview collumn from another form](http://stackoverflow.com/questions/23186519/add-a-datagridview-collumn-from-another-form) – TaW Nov 04 '15 at 19:21
  • TaW, I am confused can you please explain more about passing a reference or please show me an example. thank you – naouf Nov 04 '15 at 21:16

3 Answers3

0

SelectedRows refers to rows that are selected. You should use the Rows property instead.

  • Ryck, I changed it from SelectedRows to Rows but still did not work. any suggestion? thank you – naouf Nov 04 '15 at 18:18
  • Yes. I see you are creating a new Form1 object. You need a reference to the one you created the Form2 object from. One way to do this is to pass a reference when creating Form2. You could add a constructor that takes a Form1 reference and then save it as a class member. There are "better" ways to do this, but you aren't asking for a tutorial in design patterns. – Rÿck Nöthing Nov 04 '15 at 18:36
  • I have been searching and reading about what you said but I got confused. can you please explain more or give an example. all what I want is: to pass a value from DataGridView in form1 to a textbox in form2. thank you – naouf Nov 04 '15 at 21:10
0
  1. Create properties in Form2

    public int Qty {get; set;}

  2. 2.

  private void Edit_Line_Click(object sender, EventArgs e)   
  {
     Form2 frm2 = new Form2();
        frm2.Qty=this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
     if(frm2.ShowDialog()==DialogResult.OK)
     {
        this.dataGridView1.SelectedRows[0].Cells[0].Value = frm2.textBox1.Text;
     }
  }

This is just an idea, you have to work around to get exact output.

TaW
  • 53,122
  • 8
  • 69
  • 111
Marshal
  • 6,551
  • 13
  • 55
  • 91
0

Try this work fine for me.

Enjoy...!!!

enter image description here

I selected Row contaning Anjum text Output enter image description here

As same: enter image description here

Code Form1

using System;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(new string[] {
                    "Anjum"});
            dataGridView1.Rows.Add(new string[] {
                    "Khuram"});
            dataGridView1.Rows.Add(new string[] {
                    "Shahzad"});
        }

       

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Edit(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
   
            form2.Show();
        }
    }
}

Code Form2

using System;
using System.Windows.Forms;

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

        private void Form2_Load(object sender, EventArgs e)
        {
          
        }
        public void Edit(string Values) {
           
                textBox1.Text = Values;

           
        }
    }
}