C# question...

Associate
Joined
27 Jan 2005
Posts
1,432
Location
S. Yorks
Am trying to learn c# and am struggling!

What I have is a form with a datagrid on it, I have gone down the route of creating the datagrid dynamicly and all works fine. I am now looking to spilit the code away from the form into seperate classes and now I have done this I receive the error "Object reference not set to an instance of an object."

I have split everything out to its barest, the code was copied off MSDN.
Form1.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace dynamicDataGridControls
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Class1 cls1 = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void SetupLayout()
        {
            this.Size = new Size(600, 500);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SetupLayout();
            cls1.SetupDataGridView();
            cls1.PopulateDataGridView();

        }

    }
}

Class1.cs
Code:
            using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace dynamicDataGridControls
{
    public class Class1
    {
        public Form frm1;
        public DataGridView dGridView = new DataGridView();

        public void PopulateDataGridView()
        {

            string[] row0 = { };//{ "11/22/1968", "29", "Revolution 9", "Beatles", "The Beatles [White Album]" };
            string[] row1 = { };//{ "1960", "6", "Fools Rush In", "Frank Sinatra", "Nice 'N' Easy" };
            string[] row2 = { };//{ "11/11/1971", "1", "One of These Days", "Pink Floyd", "Meddle" };
            string[] row3 = { };//{ "1988", "7", "Where Is My Mind?", "Pixies", "Surfer Rosa" };
            string[] row4 = { };//{ "5/1981", "9", "Can't Find My Mind", "Cramps", "Psychedelic Jungle" };
            string[] row5 = { };//{ "6/10/2003", "13", "Scatterbrain. (As Dead As Leaves.)", "Radiohead", "Hail to the Thief" };
            string[] row6 = { };//{ "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };

        }
        
        public void SetupDataGridView()
        {
            frm1.Controls.Add(dGridView);

            dGridView.ColumnCount = 5;

            dGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            dGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dGridView.ColumnHeadersDefaultCellStyle.Font = new Font(dGridView.Font, FontStyle.Bold);

            dGridView.Name = "dDataGridView";
            dGridView.Location = new Point(8, 100);
            dGridView.Size = new Size(500, 250);
            dGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dGridView.GridColor = Color.Black;
            dGridView.RowHeadersVisible = false;

            dGridView.Columns[0].Name = "Release Date";
            dGridView.Columns[1].Name = "Track";
            dGridView.Columns[2].Name = "Title";
            dGridView.Columns[3].Name = "Artist";
            dGridView.Columns[4].Name = "Album";
            dGridView.Columns[4].DefaultCellStyle.Font = new Font(dGridView.DefaultCellStyle.Font, FontStyle.Italic);

            dGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dGridView.MultiSelect = false;
        }

    }
}

Like I say am totally nw to this so any help would be appreciated.

Mat
 
Last edited:
Hi,

The error is on frm1.Controls.Add(dGridView);

Sorry I am at the stage of ripping code out in frustration so have re editted the original post.

Matt
 
Thanks for that, C# is so hard work esp. when trying to learn it!

Have now moved the following to the class and it fails once more with:

Error 1 'dynamicDataGridControls.Class1' does not contain a definition for 'Size' and no extension method 'Size' accepting a first argument of type 'dynamicDataGridControls.Class1' could be found (are you missing a using directive or an assembly reference?) C:\Carriage Coster\VS New\dynamicdatagrid\dynamicDataGridControls\dynamicDataGridControls\Class1.cs 62 18 dynamicDataGridControls


Any pointers for this?

Mat
 
So I would have :

Code:
 DataGridView grid = clsForm.CreateDataGridView();
 Controls.Add(grid);

in the actual form and then the following in the class?

Code:
 public void CreateDataGridView()
        {
            DataGridView dGridView = new DataGridView();
            dGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            dGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dGridView.Name = "dDataGridView";
            dGridView.Location = new Point(130, 370);
            dGridView.Size = new Size(405, 150);
            dGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dGridView.RowHeadersVisible = false;
            dGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dGridView.MultiSelect = false;
        }

regards,

Matt
 
Ok, think I am getting somewhere, have transfered a lot of my code out to logical classes but I have now an error on this line:

Code:
                    txt.Leave += new EventHandler(txt_Exit);


Error is:
Error 1 The name 'txt_Exit' does not exist in the current context C:\Carriage Coster\VS New\CarriageCosts\CarriageCosts\clsForm.cs 101 51 CarriageCosts

I dynamicly create txtboxes and then add an eventhandler for these text boxes, the event handler is on one of the forms. So how do I do this?

Matt
 
Last edited:
Back
Top Bottom