C#DataGridView列中的意外行为

时间:2017-08-18 13:04:08

标签: c# windows visual-studio-2015 datagridview

使用Visual Studio,我创建了一个允许用户修改某些数据的数据网格视图。不幸的是,当应用程序作为可执行文件发布时,此表单似乎将数据添加到新列而不是使用指定的列。像这样:

错误如下

enter image description here

我无法在调试模式下复制错误,但这里有一幅关于它应该如何看待的图片。

预期行为如下

enter image description here

以下是创建和填充gridview的代码的一部分。

        //child name column
        DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
        nameColumn.Name = "cName";
        nameColumn.HeaderText = "Child Name";
        nameColumn.DataPropertyName = "cName";
        nameColumn.ReadOnly = true;
        dataGridView.Columns.Add(nameColumn);

        //child layer column
        var LayerColumn = new DataGridViewComboBoxColumn();
        LayerColumn.Name = "cLayer";
        LayerColumn.HeaderText = "Child Layer";
        LayerColumn.DataPropertyName = "cLayer";
        LayerColumn.AutoComplete = true;
        LayerColumn.ValueMember = "cLayer";
        LayerColumn.ValueType = typeof(string);
        dataGridView.Columns.Add(LayerColumn);

        // parent name column
        DataGridViewTextBoxColumn parentColumn = new DataGridViewTextBoxColumn();
        parentColumn.HeaderText = "Parent Name";
        parentColumn.Name = "cParent";
        parentColumn.DataPropertyName = "cParent";
        parentColumn.ReadOnly = true;
        dataGridView.Columns.Add(parentColumn);

        //parent layer column
        var pLayerColumn = new DataGridViewComboBoxColumn();
        pLayerColumn.Name = "pLayer";
        pLayerColumn.HeaderText = "Parent Layer";
        pLayerColumn.DataPropertyName = "pLayer";
        pLayerColumn.AutoComplete = true;
        pLayerColumn.ValueMember = "pLayer";
        pLayerColumn.ValueType = typeof(string);
        dataGridView.Columns.Add(pLayerColumn);

        //child name column
        DataGridViewTextBoxColumn nameColumn2 = new DataGridViewTextBoxColumn();
        nameColumn2.Name = "cName";
        nameColumn2.HeaderText = "Feat 1 Name";
        nameColumn2.DataPropertyName = "cName";
        nameColumn2.ReadOnly = true;
        dataGridView1.Columns.Add(nameColumn2);

        //child layer column
        var LayerColumn2 = new DataGridViewComboBoxColumn();
        LayerColumn2.Name = "cLayer";
        LayerColumn2.HeaderText = "Feat 1 Layer";
        LayerColumn2.DataPropertyName = "cLayer";
        LayerColumn2.AutoComplete = true;
        LayerColumn2.ValueMember = "cLayer";
        LayerColumn2.ValueType = typeof(string);
        dataGridView1.Columns.Add(LayerColumn2);

        // parent name column
        DataGridViewTextBoxColumn parentColumn2 = new DataGridViewTextBoxColumn();
        parentColumn2.HeaderText = "Feat 2 Name";
        parentColumn2.Name = "cParent";
        parentColumn2.DataPropertyName = "cParent";
        parentColumn2.ReadOnly = true;
        dataGridView1.Columns.Add(parentColumn2);

        //parent layer column
        var pLayerColumn2 = new DataGridViewComboBoxColumn();
        pLayerColumn2.Name = "pLayer";
        pLayerColumn2.HeaderText = "Feat 2 Layer";
        pLayerColumn2.DataPropertyName = "pLayer";
        pLayerColumn2.AutoComplete = true;
        pLayerColumn2.ValueMember = "pLayer";
        pLayerColumn2.ValueType = typeof(string);
        dataGridView1.Columns.Add(pLayerColumn2);

        _p = new Processor(this);

        _characteristicViewModels = new BindingList<RelativeCharacteristicViewModel>();

        dataGridView.DataSource = _characteristicViewModels;

        foreach (var c in characteristics)
        {
            foreach (var d in c.Value)
            {
                _characteristicViewModels.Add(new RelativeCharacteristicViewModel
                {
                    cName = c.Key.name,
                    cLayer = c.Key.layer,
                    cParent = d.name,
                    pLayer = d.layer
                });
            }
        }

        DataGridViewComboBoxColumn cLayerColumn = dataGridView.Columns["cLayer"] as DataGridViewComboBoxColumn;
        if (cLayerColumn != null)
        {
            layers.ForEach(p => cLayerColumn.Items.Add(p));
        }

        DataGridViewComboBoxColumn parentLayerColumn = dataGridView.Columns["pLayer"] as DataGridViewComboBoxColumn;
        if (parentLayerColumn != null)
        {
            layers.ForEach(p => parentLayerColumn.Items.Add(p));
        }

        _p = new Processor(this);

        _characteristicViewModels2 = new BindingList<RelativeCharacteristicViewModel>();

        dataGridView1.DataSource = _characteristicViewModels2;

        foreach (var c in characteristics2)
        {
            foreach (var d in c.Value)
            {
                _characteristicViewModels2.Add(new RelativeCharacteristicViewModel
                {
                    cName = c.Key.name,
                    cLayer = c.Key.layer,
                    cParent = d.name,
                    pLayer = d.layer
                });
            }
        }

        DataGridViewComboBoxColumn cLayerColumn2 = dataGridView1.Columns["cLayer"] as DataGridViewComboBoxColumn;
        if (cLayerColumn2 != null)
        {
            layers.ForEach(p => cLayerColumn2.Items.Add(p));
        }

        DataGridViewComboBoxColumn parentLayerColumn2 = dataGridView1.Columns["pLayer"] as DataGridViewComboBoxColumn;
        if (parentLayerColumn2 != null)
        {
            layers.ForEach(p => parentLayerColumn2.Items.Add(p));
        }

0 个答案:

没有答案