0

I am creating a custom WinForms control, containing a DataGridView and some buttons. I have two projects in a solution, one for the control, and one is a test application with a single form containing this control. The control is called PagedDataGridView.

I have built the entire solution and added the required reference for the custom control to the test application project. It was working fine previously, but all of a sudden it throws a System.ArgumentNullException when trying to run the test application.

This Program.cs from the test application project:

namespace testApp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1()); // stack trace refers to this line
        }
    }
}

This is code for Form1.cs

namespace testApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); // stack trace refers to this line
        }
    }
}

This is Form1.designer.cs

namespace testApp
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pagedDataGridView1 = new PagedDataGridView.PagedDataGridView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button_togglePaging = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pagedDataGridView1
            // 
            this.pagedDataGridView1.currentPage = 0;
            this.pagedDataGridView1.dataSource = null;
            this.pagedDataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pagedDataGridView1.height = 368;
            this.pagedDataGridView1.Location = new System.Drawing.Point(3, 18);
            this.pagedDataGridView1.Name = "pagedDataGridView1";
            this.pagedDataGridView1.pageSize = 0; // stack trace refers to this line
            this.pagedDataGridView1.pagingEnabled = false;
            this.pagedDataGridView1.Size = new System.Drawing.Size(770, 368);
            this.pagedDataGridView1.TabIndex = 0;
            this.pagedDataGridView1.width = 770;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.pagedDataGridView1);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(776, 389);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "PagedDataGridView";
            // 
            // button_togglePaging
            // 
            this.button_togglePaging.Location = new System.Drawing.Point(15, 407);
            this.button_togglePaging.Name = "button_togglePaging";
            this.button_togglePaging.Size = new System.Drawing.Size(129, 31);
            this.button_togglePaging.TabIndex = 2;
            this.button_togglePaging.Text = "Toggle paging";
            this.button_togglePaging.UseVisualStyleBackColor = true;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.button_togglePaging);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private PagedDataGridView.PagedDataGridView pagedDataGridView1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button button_togglePaging;
    }
}

What is causing this?

More information: More code is provided from the custom control:

// constructor for the custom control
public PagedDataGridView()
{
    InitializeComponent()

    // subscribe to events
    this.pageSizeChanged += PagedDataGridView_pageSizeChanged;
    this.currPageChanged += PagedDataGridView_currPageChanged;
    this.pagingEnabledChanged += PagedDataGridView_pagingEnabledChanged;

    pageSize = 1; // ArgumentNullException here

    // no data source yet
    dataSource = null;

    this.pagingEnabled = false;
    OnPagingEnabledChanged(new PagingEnabledChangedEventArgs(_pagingEnabled));
}

// this is the property 
private int _pageSize;
public int pageSize
{
    get
    {
         return _pageSize;
    }
    set
    {
        _pageSize = value;
        // fire the page size changed event
        OnPageSizeChanged(new PageSizeChangedEventArgs(_pageSize));
    }
}

// event
protected virtual void OnPageSizeChanged(PageSizeChangedEventArgs args)
{
    pageSizeChanged?.Invoke(this, args);
    // ArgumentNullException here
}
private void PagedDataGridView_pageSizeChanged(object sender, PageSizeChangedEventArgs args)
{
    numberOfPages = pageSize*2;

    currentPage = 1; // ArgumentNullException here

    updateCurrPageIndicator();
}
Al2110
  • 566
  • 9
  • 25
  • What is `PagedDataGridView`? Its properties names imply it's your own control. You have to show it. Also, make sure to follow [naming guidelines](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines). – Sinatr Jun 23 '20 at 07:14
  • you write: "This is Form1.designer.cs" but the code clearly says: `partial class Form2`. So which one is correct? – Mong Zhu Jun 23 '20 at 09:16
  • @MongZhu it should all be `Form1`. Updated. – Al2110 Jun 23 '20 at 22:54
  • We most likely have to see the PagedDataGridView constructor or the property information related to PageSize. – LarsTech Jun 23 '20 at 23:04
  • @LarsTech more relevant code has been included in the original post. – Al2110 Jun 24 '20 at 04:28
  • @MongZhu more relevant code has been included in the original post. – Al2110 Jun 24 '20 at 04:28
  • @Sinatr more relevant code has been included in the original post. – Al2110 Jun 24 '20 at 04:28
  • Try moving `pageSize = 1;` above the "subscribe to events" section, or change it to `_pageSize = 1;` See if that works. – LarsTech Jun 24 '20 at 13:17
  • @LarsTech after trying both, neither worked. – Al2110 Jun 26 '20 at 01:23

0 Answers0