Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

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

$
0
0

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));
        }

    }


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>