4 Replies - 111612 Views - Last Post: 21 May 2009 - 02:05 PM Rate Topic: -----

#1 getajob1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 03-February 09

Adding a column to DataGridView

Post icon  Posted 16 February 2009 - 09:52 AM


I am trying to add a column to my DataGridView then fill it with values at a later time.
If I add a checkbox column it's not a problem, but adding a regular column gives
me a celltype property is null error. Any ideas around this? Thanks


DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn(); // add a check box column to the grid
			cb.HeaderText = "test";
			cb.Name = "test";
			cb.Visible = true;
			cb.Width = 40;

			gridColors.Columns.Add(cb);

		   
			DataGridViewColumn  newCol = new DataGridViewColumn(); // add a column to the grid
			newCol.HeaderText = "test2";
			newCol.Name = "test2";
			newCol.Visible = true;
			newCol.Width = 40;
			
			gridColors.Columns.Add(newCol);   (THE PROBLEM IS HERE, IT GIVES ME A CELLTYPE IS NULL ERROR)

					  Column cannot be added because its CellType property is null



Is This A Good Question/Topic? 0
  • +

Replies To: Adding a column to DataGridView

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Adding a column to DataGridView

Posted 16 February 2009 - 11:18 AM

You need to specify what type of cell the column will contain.

			DataGridViewColumn  newCol = new DataGridViewColumn(); // add a column to the grid
			DataGridViewCell cell = new DataGridViewCell(); //Specify which type of cell in this column
			newCol.CellTemplate = cell;

			newCol.HeaderText = "test2";
			newCol.Name = "test2";
			newCol.Visible = true;
			newCol.Width = 40;
		   
			gridColors.Columns.Add(newCol);

Was This Post Helpful? 0
  • +
  • -

#3 getajob1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 03-February 09

Re: Adding a column to DataGridView

Posted 17 February 2009 - 09:14 AM

[b]
I added the code you said but I get an error on DataGridViewCell newCell = new DataGridViewCell();
the error I get is Error 1 Cannot create an instance of the abstract class or interface 'System.Windows.Forms.DataGridViewCell'

Any other ideas on how to get around this? Thanks
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Adding a column to DataGridView

Posted 17 February 2009 - 09:32 AM

that's because you can't create a new instance of "DataGridViewCell". It has to be a typed cell.

DataGridViewColumn col = new DataGridViewColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell();
col.CellTemplate = cell;
col.HeaderText = "testing";
col.Name = "testing";
col.Visible = true;
col.Width = 60;



the cell is now a "DataGridViewTextBoxCell".
Was This Post Helpful? 1

#6 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: Adding a column to DataGridView

Posted 21 May 2009 - 02:05 PM

i have a gridview, and i will add column to it, but i want to add a column with full row, not row by row.
wht should i do?

thx
Was This Post Helpful? 1

Page 1 of 1