无法添加列,因为其CellType属性为null异常

Nik*_*lin 13 c# datagridview

我遇到以下代码问题.当我使用调试器时,我遇到以下行的异常:

dgvCalls.Columns.Insert(1, msisnnColumn);
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

无法添加列,因为其CellType属性为null.

奇怪的是,我为其他一些DataGridViews创建了相同的程序,它运行正常.

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();
                }
Run Code Online (Sandbox Code Playgroud)

有什么建议?

BFr*_*ree 18

dgvCalls.Columns.Insert(1, msisnnColumn);
msisnnColumn.CellTemplate = new DataGridViewTextBoxCell();
Run Code Online (Sandbox Code Playgroud)

尝试翻转这两行.这可能会成功.