Associate
- Joined
- 27 Jan 2005
- Posts
- 1,397
- 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
Class1.cs
Like I say am totally nw to this so any help would be appreciated.
Mat
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: