locked
Drag n drop fails - Object reference not set to an instance of an object RRS feed

  • Question

  • I did follow a Drag N Drop tutorial on YouTube, did exactly the same thing but I get the error:

    "Object reference not set to an instance of an object".

    The difference would be that I do create the DataGridView in code behind. Also there's several of them. One in each tab.

    How do I adjust my code to make it work?

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                tabControl1.TabPages.Clear();
                createGridViews();
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                dgwDynamic.Columns["Column1"].Visible = false;
            }
    
            DataGridView dgwDynamic;
            private void createGridViews()
            {
                int pageNumber = 0;
                for (int i = 0; i < 3; i++)
                {
                    //Populate DataGridView.
    
                    /* Dynamic DataGridView */
                    dgwDynamic = new DataGridView();
                    dgwDynamic.Name = "DataGridView" + i.ToString();
                    dgwDynamic.ScrollBars = ScrollBars.Both;
                    dgwDynamic.Dock = DockStyle.Fill;
    
                    /* Dynamic TabPage */
                    TabPage tpDynamic = new TabPage();
                    pageNumber = i + 1;
                    tpDynamic.Name = "tabPage" + pageNumber.ToString();
    
                    string tabName = "TabPage" + pageNumber.ToString();
    
                    tpDynamic.Text = tabName;
    
                    tpDynamic.TabIndex = i;
                    // One newGridView per newTabPage
                    tpDynamic.Controls.Add(dgwDynamic); // Add Dynamic DataGridView to Dynamic TabPage
                    tabControl1.Controls.Add(tpDynamic); // Add Dynamic TabPage to TabControl
                    dgwDynamic.DataSource = GetTable(); // Add DataTable to Dynamic DataGridView
                    /* Drag N Drop EventHandler */
                    dgwDynamic.CellMouseDown += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgwDynamic_CellMouseDown);
    
                }//End for loop
            }
    
            private static DataTable table;
            static DataTable GetTable()
            {
                // Here we create a DataTable with four columns.
                table = new DataTable();
                table.Columns.Add("Column1", typeof(string));
                table.Columns.Add("Column2", typeof(string));
                table.Columns.Add("Column3", typeof(string));
    
    
                // Here we add five DataRows.
                table.Rows.Add("Col1_Row1", "Col2_Row1", "Col3_Row1");
                table.Rows.Add("Col1_Row2", "Col2_Row2", "Col3_Row2");
                table.Rows.Add("Col1_Row3", "Col2_Row3", "Col3_Row3");
                table.AcceptChanges();
                return table;
            }
    
            private void dgwDynamic_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                //To Copy the data selected
                dgwDynamic.DoDragDrop(dgwDynamic.CurrentCell.Value.ToString(), DragDropEffects.Copy);
                // ERROR: Object reference not set to an instance of an object
            }
    
            private void listBox1_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.Text))
                    e.Effect = DragDropEffects.Copy;
                else
                    e.Effect = DragDropEffects.None;
            }
    
            private void listBox1_DragDrop(object sender, DragEventArgs e)
            {
                listBox1.Items.Add(e.Data.GetData(DataFormats.Text));
            }
    
        }

    Sunday, January 24, 2016 2:44 PM

Answers

  • Hi Jonas Andersson,

    >>Can you post the whole code that you used for this?

    Here is my code(I have one checkbox, one listbox, one tabcontrol in the form):

    public partial class Form1 : Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
                tabControl1.TabPages.Clear();
    
                createGridViews();
    
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
    
            {
    
                //dgwDynamic.Columns["Column1"].Visible = false;
    
                foreach (Control con in tabControl1.Controls)
    
                {
    
                    if (con is TabPage)
    
                    {
    
                        foreach (Control c in con.Controls)
    
                        {
    
                            if (c is DataGridView)
    
                            {
    
                               (c as DataGridView).Columns["Column1"].Visible = (sender as CheckBox).Checked;
    
                            }
    
                        }
    
                    }
    
                }
    
            }
    
            DataGridView dgwDynamic;
    
            private void createGridViews()
    
            {
    
                int pageNumber = 0;
    
                for (int i = 0; i < 3; i++)
    
                {
    
                    //Populate DataGridView.
    
                    /* Dynamic DataGridView */
    
                    dgwDynamic = new DataGridView();
    
                    dgwDynamic.Name = "DataGridView" + i.ToString();
    
                    dgwDynamic.ScrollBars = ScrollBars.Both;
    
                    dgwDynamic.Dock = DockStyle.Fill;
    
                    /* Dynamic TabPage */
    
                    TabPage tpDynamic = new TabPage();
    
                    pageNumber = i + 1;
    
                    tpDynamic.Name = "tabPage" + pageNumber.ToString();
    
                    string tabName = "TabPage" + pageNumber.ToString();
    
                    tpDynamic.Text = tabName;
    
                    tpDynamic.TabIndex = i;
    
                    // One newGridView per newTabPage
    
                    tpDynamic.Controls.Add(dgwDynamic); // Add Dynamic DataGridView to Dynamic TabPage
    
                    tabControl1.Controls.Add(tpDynamic); // Add Dynamic TabPage to TabControl
    
                    dgwDynamic.DataSource = GetTable(tpDynamic.Name); // Add DataTable to Dynamic DataGridView
    
                    /* Drag N Drop EventHandler */
    
                    dgwDynamic.CellMouseDown += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgwDynamic_CellMouseDown);
    
                }//End for loop
    
            }
    
            private static DataTable table;
    
            static DataTable GetTable(string tpName)
    
            {
    
                // Here we create a DataTable with four columns.
    
                table = new DataTable();
    
                table.Columns.Add("Column1", typeof(string));
    
               table.Columns.Add("Column2", typeof(string));
    
                table.Columns.Add("Column3", typeof(string));
    
                // Here we add five DataRows.
    
                table.Rows.Add(tpName + "Col1_Row1", tpName + "Col2_Row1", tpName + "Col3_Row1");
    
                table.Rows.Add(tpName + "Col1_Row2", tpName + "Col2_Row2", tpName + "Col3_Row2");
    
                table.Rows.Add(tpName + "Col1_Row3", tpName + "Col2_Row3", tpName + "Col3_Row3");
    
                table.AcceptChanges();
    
                return table;
    
            }
    
            private void dgwDynamic_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    
            {
    
                if ((sender as DataGridView).CurrentCell != null)
    
                {
    
                    //get the currect datagridview which you select
    
                    DataGridView dgv = sender as DataGridView;
    
                    //To Copy the data selected
    
                    //dgwDynamic.DoDragDrop(dgwDynamic.CurrentCell.Value.ToString(), DragDropEffects.Copy);
    
                    dgwDynamic.DoDragDrop(dgv.CurrentCell.Value.ToString(), DragDropEffects.Copy);
    
                    // ERROR: Object reference not set to an instance of an object
    
                }
    
            }
    
            private void listBox1_DragEnter(object sender, DragEventArgs e)
    
            {
    
                if (e.Data.GetDataPresent(DataFormats.Text))
    
                    e.Effect = DragDropEffects.Copy;
    
                else
    
                    e.Effect = DragDropEffects.None;
    
            }
    
            private void listBox1_DragDrop(object sender, DragEventArgs e)
    
            {
    
                listBox1.Items.Add(e.Data.GetData(DataFormats.Text));
    
            }
    
        }
    

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, February 1, 2016 2:44 AM

All replies


  •         private void dgwDynamic_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                //To Copy the data selected
                dgwDynamic.DoDragDrop(dgwDynamic.CurrentCell.Value.ToString(), DragDropEffects.Copy);
                // ERROR: Object reference not set to an instance of an object
            }
    

    Which object is null? dgwDynamic, or CurrentCell or Value? [Set a Breakpoint to that line and Hover the mouse over each of the entities, look what intellisense says...]

    Regards,

      Thorsten


    Monday, January 25, 2016 2:58 AM
  • Hi Jonas Andersson,
    I have tested your code, I found the issue is caused by the following code.     

    dgwDynamic.DoDragDrop(dgwDynamic.CurrentCell.Value.ToString(), DragDropEffects.Copy);

    Your datagridviews are dynamic create, it only works in tabpage3.
    I have modified your code and the application worked well, here is the code:

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                //dgwDynamic.Columns["Column1"].Visible = false;
                foreach (Control con in tabControl1.Controls)
                {
                    if (con is TabPage)
                    {
                        foreach (Control c in con.Controls)
                        {
                            if (c is DataGridView)
                            {
                               (c as DataGridView).Columns["Column1"].Visible = (sender as CheckBox).Checked;
                            }
                        }
                    }
                }
            }
    

    Screenshots:

    Drag Drop Column

    Hide Column

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, January 25, 2016 6:10 AM
  • OK, you solved the hide column issue, but that's another story.

    It is the Drag N Drop that I was wondering about, but I found out I did missed an important step and that was to set "AllowDrop=True" for the listbox.

    But as you said, it only works for tabpage 3.

    As soon as I click any cell in tabpage 1 or 2 I get:

    An unhandled exception of type 'System.NullReferenceException' occurred in Sandbox.exe
    
    Additional information: Object reference not set to an instance of an object.

    Or if I first click on tabpage 3, then It works for the other tabs as well but not quite. No matter which cell I pick in tab 1 and 2 it ends up draging the first cell all the time. This is not the case for tabpage 3, where the correct cells are being dragged to the list.

    Looking at the screens dumps it looks like it works for you. Can you post the whole code that you used for this?


    Tuesday, January 26, 2016 12:30 AM
  • Hi Jonas Andersson,

    >>Can you post the whole code that you used for this?

    Here is my code(I have one checkbox, one listbox, one tabcontrol in the form):

    public partial class Form1 : Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
                tabControl1.TabPages.Clear();
    
                createGridViews();
    
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
    
            {
    
                //dgwDynamic.Columns["Column1"].Visible = false;
    
                foreach (Control con in tabControl1.Controls)
    
                {
    
                    if (con is TabPage)
    
                    {
    
                        foreach (Control c in con.Controls)
    
                        {
    
                            if (c is DataGridView)
    
                            {
    
                               (c as DataGridView).Columns["Column1"].Visible = (sender as CheckBox).Checked;
    
                            }
    
                        }
    
                    }
    
                }
    
            }
    
            DataGridView dgwDynamic;
    
            private void createGridViews()
    
            {
    
                int pageNumber = 0;
    
                for (int i = 0; i < 3; i++)
    
                {
    
                    //Populate DataGridView.
    
                    /* Dynamic DataGridView */
    
                    dgwDynamic = new DataGridView();
    
                    dgwDynamic.Name = "DataGridView" + i.ToString();
    
                    dgwDynamic.ScrollBars = ScrollBars.Both;
    
                    dgwDynamic.Dock = DockStyle.Fill;
    
                    /* Dynamic TabPage */
    
                    TabPage tpDynamic = new TabPage();
    
                    pageNumber = i + 1;
    
                    tpDynamic.Name = "tabPage" + pageNumber.ToString();
    
                    string tabName = "TabPage" + pageNumber.ToString();
    
                    tpDynamic.Text = tabName;
    
                    tpDynamic.TabIndex = i;
    
                    // One newGridView per newTabPage
    
                    tpDynamic.Controls.Add(dgwDynamic); // Add Dynamic DataGridView to Dynamic TabPage
    
                    tabControl1.Controls.Add(tpDynamic); // Add Dynamic TabPage to TabControl
    
                    dgwDynamic.DataSource = GetTable(tpDynamic.Name); // Add DataTable to Dynamic DataGridView
    
                    /* Drag N Drop EventHandler */
    
                    dgwDynamic.CellMouseDown += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgwDynamic_CellMouseDown);
    
                }//End for loop
    
            }
    
            private static DataTable table;
    
            static DataTable GetTable(string tpName)
    
            {
    
                // Here we create a DataTable with four columns.
    
                table = new DataTable();
    
                table.Columns.Add("Column1", typeof(string));
    
               table.Columns.Add("Column2", typeof(string));
    
                table.Columns.Add("Column3", typeof(string));
    
                // Here we add five DataRows.
    
                table.Rows.Add(tpName + "Col1_Row1", tpName + "Col2_Row1", tpName + "Col3_Row1");
    
                table.Rows.Add(tpName + "Col1_Row2", tpName + "Col2_Row2", tpName + "Col3_Row2");
    
                table.Rows.Add(tpName + "Col1_Row3", tpName + "Col2_Row3", tpName + "Col3_Row3");
    
                table.AcceptChanges();
    
                return table;
    
            }
    
            private void dgwDynamic_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    
            {
    
                if ((sender as DataGridView).CurrentCell != null)
    
                {
    
                    //get the currect datagridview which you select
    
                    DataGridView dgv = sender as DataGridView;
    
                    //To Copy the data selected
    
                    //dgwDynamic.DoDragDrop(dgwDynamic.CurrentCell.Value.ToString(), DragDropEffects.Copy);
    
                    dgwDynamic.DoDragDrop(dgv.CurrentCell.Value.ToString(), DragDropEffects.Copy);
    
                    // ERROR: Object reference not set to an instance of an object
    
                }
    
            }
    
            private void listBox1_DragEnter(object sender, DragEventArgs e)
    
            {
    
                if (e.Data.GetDataPresent(DataFormats.Text))
    
                    e.Effect = DragDropEffects.Copy;
    
                else
    
                    e.Effect = DragDropEffects.None;
    
            }
    
            private void listBox1_DragDrop(object sender, DragEventArgs e)
    
            {
    
                listBox1.Items.Add(e.Data.GetData(DataFormats.Text));
    
            }
    
        }
    

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, February 1, 2016 2:44 AM