Add row to dataGridView

tellblom

Member
Joined
Jul 3, 2022
Messages
16
Programming Experience
10+
Have a dataGridView (without a datasource) with four columns.
When I try to programmaticly add a new row I get this error in Visual Studio:
CS0120: An object reference is required for the non-static field, method or property 'Form1.dataGridView1' and it wont compile.

I have done exactly the same before but in VS2022 it does not work.

Whats wrong?
 
Solution
When you use static on line 30, that means you are not referencing a specific instance of a class. Therefore this on line 33 doesn't know what instance you are referencing. On line 34, Form1 refers to the entire class, but InfoGrid is an instance member of the class, so you need to reference a specific instance.

Easiest solution is to remove line 34, and remove static from line 30.
You need to access the instance of Form1. C# is not like VB where it figures out the instance for you.
 
Show us the code you're using and tell us where it is and what the relationship is between that form and the form containing the grid. I'm guessing that Form1 is your startup form and it creates the form containing that code but who knows?
 
There is only one form = Form1
The DataGridView component named InfoGrid is placed in that form


InForm1.Designer.cs this is how it looks:
C#:
    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.label1 = new System.Windows.Forms.Label();
            this.dateTimePickerFromDate = new System.Windows.Forms.DateTimePicker();
            this.dateTimePickerToDate = new System.Windows.Forms.DateTimePicker();
            this.label2 = new System.Windows.Forms.Label();
            this.InfoGrid = new System.Windows.Forms.DataGridView();
            this.A = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.B = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.C = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.D = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.InfoGrid)).BeginInit();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(20, 30);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(84, 20);
            this.label1.TabIndex = 0;
            this.label1.Text = "Från datum";
            //
            // dateTimePickerFromDate
            //
            this.dateTimePickerFromDate.Location = new System.Drawing.Point(104, 29);
            this.dateTimePickerFromDate.Name = "dateTimePickerFromDate";
            this.dateTimePickerFromDate.Size = new System.Drawing.Size(110, 27);
            this.dateTimePickerFromDate.TabIndex = 1;
            this.dateTimePickerFromDate.ValueChanged += new System.EventHandler(this.dateTimePickerFromDate_ValueChanged);
            //
            // dateTimePickerToDate
            //
            this.dateTimePickerToDate.Location = new System.Drawing.Point(104, 62);
            this.dateTimePickerToDate.Name = "dateTimePickerToDate";
            this.dateTimePickerToDate.Size = new System.Drawing.Size(110, 27);
            this.dateTimePickerToDate.TabIndex = 3;
            this.dateTimePickerToDate.ValueChanged += new System.EventHandler(this.dateTimePickerToDate_ValueChanged);
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(20, 63);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(76, 20);
            this.label2.TabIndex = 2;
            this.label2.Text = "Till datum";
            //
            // InfoGrid
            //
            this.InfoGrid.AllowUserToAddRows = false;
            this.InfoGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.InfoGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.A,
            this.B,
            this.C,
            this.D});
            this.InfoGrid.Location = new System.Drawing.Point(12, 241);
            this.InfoGrid.Name = "InfoGrid";
            this.InfoGrid.RowHeadersWidth = 51;
            this.InfoGrid.RowTemplate.Height = 29;
            this.InfoGrid.ShowEditingIcon = false;
            this.InfoGrid.Size = new System.Drawing.Size(734, 188);
            this.InfoGrid.TabIndex = 4;
            this.InfoGrid.VirtualMode = true;
            //
            // A
            //
            this.A.HeaderText = "A";
            this.A.MinimumWidth = 6;
            this.A.Name = "A";
            this.A.Width = 125;
            //
            // B
            //
            this.B.HeaderText = "B";
            this.B.MinimumWidth = 6;
            this.B.Name = "B";
            this.B.Width = 125;
            //
            // C
            //
            this.C.HeaderText = "C";
            this.C.MinimumWidth = 6;
            this.C.Name = "C";
            this.C.Width = 125;
            //
            // D
            //
            this.D.HeaderText = "D";
            this.D.MinimumWidth = 6;
            this.D.Name = "D";
            this.D.Width = 125;
                  
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.InfoGrid);
            this.Controls.Add(this.dateTimePickerToDate);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.dateTimePickerFromDate);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Make Reports";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.InfoGrid)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private DataGridView InfoGrid;
        private Label label1;
        private DateTimePicker dateTimePickerFromDate;
        private DateTimePicker dateTimePickerToDate;
        private Label label2;
        
    }
}


And this is Form1.cs
C#:
namespace Make_reports
{
    public partial class Form1 : Form
    {
        private static class Global
        {
 
            public static string FromDate = "";
            public static string ToDate = "";
            public static List<string> Kundnr = new List<string>();
        }
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePickerFromDate.Format = DateTimePickerFormat.Custom;
            dateTimePickerFromDate.CustomFormat = "yyyy-MM-dd";
            dateTimePickerToDate.Format = DateTimePickerFormat.Custom;
            dateTimePickerToDate.CustomFormat = "yyyy-MM-dd";

            dateTimePickerFromDate.Value = DateTime.Today;
            dateTimePickerToDate.Value = DateTime.Today;

        }
        
        static void AddLine(string A, string B, string C, string D)
        {

            this.InfoGrid.Rows.Add(A,B,C,D);
            Form1.InfoGrid.Rows.Add(A,B,C,D);

          

        }

Both of this lines gives the same error
this.InfoGrid.Rows.Add(A,B,C,D);
Form1.InfoGrid.Rows.Add(A,B,C,D);
 
When you use static on line 30, that means you are not referencing a specific instance of a class. Therefore this on line 33 doesn't know what instance you are referencing. On line 34, Form1 refers to the entire class, but InfoGrid is an instance member of the class, so you need to reference a specific instance.

Easiest solution is to remove line 34, and remove static from line 30.
 
Solution
When you use static on line 30, that means you are not referencing a specific instance of a class. Therefore this on line 33 doesn't know what instance you are referencing. On line 34, Form1 refers to the entire class, but InfoGrid is an instance member of the class, so you need to reference a specific instance.

Easiest solution is to remove line 34, and remove static from line 30.

Thanks remove static did the trick.
 
Back
Top Bottom