-2

i have two dataGridViews in my windows form and for each gridview buttons to edit the content of the cell. however when i start my application i always get a System.NullReferenceException when i don't click on a row in the view first

The row is selected and blue after i started the application: selected row

I set the following in my load method:

            dataGridView1.Rows[0].Selected = true;
            dataGridView2.Rows[0].Selected = true;

The second DatagridView works fine. I don't know why...

That is my code for one button:

private void button2_Click(object sender, EventArgs e)
    {
        if (dataGridView1.SelectedRows.Count > 0)
        {
            int _selectedArticle_ID, _selectedArticle_ArtNr;
            string _selectedArticle_Artikel, _selectedArticle_Barcode;
            decimal _selectedArticle_einkaufsPreis, _selectedArticle_verkaufsPreis;

            _selectedArticle_ID = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value);
            _selectedArticle_ArtNr = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value);
            _selectedArticle_Artikel = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString();
            _selectedArticle_einkaufsPreis = Convert.ToDecimal(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[3].Value);
            _selectedArticle_verkaufsPreis = Convert.ToDecimal(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4].Value);
            _selectedArticle_Barcode = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value.ToString();

            f3 = new Form3(); // (_selectedArticle_ID, _selectedArticle_Anz, _selectedArticle_ArtNr, _selectedArticle_Artikel, _selectedArticle_Preis, _selectedArticle_Barcode, this);
            f3.Show();
        }
    }
Stephan S.
  • 21
  • 5

1 Answers1

0

There is no datagrid to check for selected rows. So this line throws an error because of the null value: if (dataGridView1.SelectedRows.Count > 0)

You could throw in something like if (dataGridView1 == null) { return; } to check first and exit the method if dataGridView1 is still null.

Lothar
  • 23
  • 5
  • but the row is selected and blue after i started the application – Stephan S. Jan 15 '18 at 19:52
  • Hmm. The method posted seems to be for dataGridView1 only. Is there a separate button click method for dataGridView2? If so, are there any differences in the methods? – Lothar Jan 15 '18 at 20:07
  • for datagridview2 is a extra button but its exactly the same code .. the line is blue and selected .. i klick the button the form 3 pops up .. not for datagridview1 – Stephan S. Jan 15 '18 at 20:09
  • How are the dataGridViews being populated? Are the contents of dataGridView1 and dataGridView2 different? If so, does the error happen when their contents are the same? I guess those would be the next things I'd check. – Lothar Jan 15 '18 at 20:25
  • the content in it is different but .. the methods are exactly the same... i think it is because the second gridview is in focus or something .. – Stephan S. Jan 15 '18 at 20:29
  • Maybe this is because of the use of CurrentCell. You are setting the selected rows, but not the selected cell. So in `_selectedArticle_ID = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value);` the CurrentCell is null. You might try changing this `dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex]` to this `dataGridView1.SelectedRows[0]` to see if that makes a difference. – Lothar Jan 15 '18 at 20:43