0
  • DataGridView - Placed in Form - 2
  • TextBox1 - Placed in Form - 1

I want to access Data Grid View Row and Column value in Form - 1 and display in textbox.

I'm writing this code below:

Form2 form2 = new Form2();
DataGridView data = new DataGridView();
data = form2.qualitySetupDataGridView;

MessageBox.Show(data.Rows[0].Cells[1].Value.ToString());

Error Message: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Note: DataGridView Modifiers is set to public. I have 3-4 Records in the DataGridView that is placed in form-2.

4
  • Is that code in the Form1 source? Does it mean you are instantiating Form2 from Form1? Feb 19, 2015 at 11:08
  • You are making an new instance of Form2 in your Form1, thus having an empty DatagridView. You should pass the current instance of DatagridView on your Form1 when you open it in Form2
    – apomene
    Feb 19, 2015 at 11:16
  • @JCabello Thats correct.
    – Fawad
    Feb 19, 2015 at 11:21
  • @apomene: code will be appreciated. I'm thinking form2.qualitySetupDataGridView is doing the job. so its empty datagridview no wonder.
    – Fawad
    Feb 19, 2015 at 11:22

1 Answer 1

1

You are making an new instance of Form2 in your Form1, thus having an empty DatagridView. You should pass the current instance of DatagridView , e.g:

Form1:

Fawad Edited:

You forgot to create object: this does the job, but it pops up form 2 for a second to get data and then recloses, which isn't perfect but gets the job done.

Form2 f2 = new Form2();
Form2.Show();
Form2.Hide();

Form2 f = Application.OpenForms.OfType<Form2>().ElementAt<Form2>(0); //Get current open Form2
DataGridView data = f.qualitySetupDataGridView;
MessageBox.Show(data.Rows[0].Cells[1].Value.ToString());
10
  • after writing this code, now I'm getting a different error. System.ArgumentOutOfRangeException: Specified argument was out of range of valid values. Parameter: Index.
    – Fawad
    Feb 19, 2015 at 11:43
  • is there any open Form2??is there a datagridVIew in Form2 instatinated?
    – apomene
    Feb 19, 2015 at 11:45
  • just 1 here. this is in form - 1 private void qualitySetupToolStripMenuItem_Click(object sender, EventArgs e) { // Quality Setup Form Goes here. // This updates ComboBox Value with new-Added Data in Quality Setup. new Form2().ShowDialog(); this.qualitySetupTableAdapter.Fill(this.setupsDBDataSet.QualitySetup); this.comboBox1.SelectedIndex = -1; }
    – Fawad
    Feb 19, 2015 at 11:47
  • I got your point. by doing this Form2 form2 = new Form(); DataGridView data = new DataGrid View; data = form2.qualitySetupDataGridView; I'm creating a new empty form and linking a new empty datagridview. However my debugger is throwing exception at this line: Form2 f = Application.OpenForms.OfType<Form2>().ElementAt<Form2>(0);
    – Fawad
    Feb 19, 2015 at 11:55
  • Just for your information: I have 3 forms, but form 3 is doing nothing basically just pops up help and displays application information.
    – Fawad
    Feb 19, 2015 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.