Search code examples
c#.netdatagridviewdatagridviewcolumn

Why can't a cell indexer be mapped to a const int?


With this code:

private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            const int TICKETSOURCE_COLUMN = 3;
            const int ABOUTSOMEID_COLUMN = 4;
            const int CATEGORYID_COLUMN = 5;
            const int CONTACTEMAIL_COLUMN = 6;

            textBoxTicketSource.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[TICKETSOURCE_COLUMN].Value);
            textBoxAboutLLSID.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[ABOUTSOMEID_COLUMN].Value);
            textBoxCategoryID.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[CATEGORYID_COLUMN].Value);
            textBoxContactEmail.Text = Convert.ToString(dataGridView1.CurrentRow.Cells[CONTACTEMAIL_COLUMN].Value); 
        }

I'm getting this err msg:

"System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object."

I have two questions about this:

1) Why is this a problem/how should I do this instead?

2) Is the RowEnter() event the best place to put this code?


Solution

  • The const int is not the problem here...

    Either you have mis-numbered them or, more likely, CurrentRow is null

    Post the stacktrace, and simply use the debugger.