Thursday, February 17, 2011

Column cannot be added because its CellType property is null exception

Hi,

I have trouble with the following piece of code. When I go through with the debugger I get an exception when it comes to the following line:

dgvCalls.Columns.Insert(1, msisnnColumn);

I get an exception:

Column cannot be added because its CellType property is null.

Oddly, I created the same procedure for some other DataGridViews and it worked fine.

if (!(dgvCalls.Columns.Contains("DirectionImage")))
                {
                    directionIconColumn = new DataGridViewImageColumn();
                    directionIconColumn.Name = "DirectionImage";
                    directionIconColumn.HeaderText = "";
                    dgvCalls.Columns.Insert(0, directionIconColumn);
                    directionIconColumn.CellTemplate = new DataGridViewImageCell();
                }
                if (!(dgvCalls.Columns.Contains("msisndColumn")))
                {
                    msisnnColumn = new DataGridViewColumn();
                    msisnnColumn.Name = "msisndColumn";
                    msisnnColumn.HeaderText = "Klic";
                    dgvCalls.Columns.Insert(1, msisnnColumn);
                    msisnnColumn.CellTemplate = new DataGridViewTextBoxCell();
                }

Any suggestions?

From stackoverflow
  • dgvCalls.Columns.Insert(1, msisnnColumn);
    msisnnColumn.CellTemplate = new DataGridViewTextBoxCell();
    

    Try flipping those two lines. That might do the trick.

    niko : Thanks! It works. I wonder why it works in the other form where the order is different!
    BFree : The thing is, since you're just assigning msisnnColumn to a new DataGridViewColumn() it doesn't know what CellType you want, so it just leaves it null. Therefore when you went to do the insert, it blew up. However, if you first assign the CellTemplate to a TextBoxCell, it knows what you want.
  • its working... really thanks.....

0 comments:

Post a Comment