I am trying to create a datagridview on an event. But I have ran into strange error.
System.NullReferenceException was unhandled Message=Object reference
not set to an instance of an object.
A part of a code:
//Creating table
System::Windows::Forms::DataGridView^ dataGridView1;
System::Windows::Forms::DataGridViewTextBoxColumn^ Column1;
System::Windows::Forms::DataGridViewTextBoxColumn^ Column2;
dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
//
// dataGridView1
//
dataGridView1->AllowUserToAddRows = false;
dataGridView1->AllowUserToDeleteRows = false;
dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^ >(2) {Column1,
Column2});
The error is shown in the last line of the code above.
It seams to be that I have made mistake while defining an object or have forgotten to define something something else.
Where have I made a mistake?
I am using Visual studio 2010 and working on a Windows frames project.
Your column objects are declared but not instantiated. You need to gcnew them like dataGridView1. You’re getting a NullReference exception because you’re working with uninstantiated objects on that last line. Adjust your code like this: