C# 添加DataGridViewComboxColumn-当网格刷新列的索引时更改

C# 添加DataGridViewComboxColumn-当网格刷新列的索引时更改,c#,datagridview,C#,Datagridview,我在向DataGridView添加DataGridViewComboBoxColumn时遇到了一些奇怪的行为。这是我的密码: private void CreateSupplyTypeColumn() { supplyTypeCombo = new DataGridViewComboBoxColumn(); supplyTypeCombo.HeaderText = "Circuit Type";

我在向DataGridView添加DataGridViewComboBoxColumn时遇到了一些奇怪的行为。这是我的密码:

        private void CreateSupplyTypeColumn()
        {
            supplyTypeCombo = new DataGridViewComboBoxColumn();
            supplyTypeCombo.HeaderText = "Circuit Type";
            supplyTypeCombo.Name = "colCircuitType";
            supplyTypeCombo.DataSource = supplyType;
            supplyTypeCombo.DisplayMember = "SupplyTypeShort";
            supplyTypeCombo.ValueMember = "SupplyTypeID";
            dgDeliveryPoints.Columns.Insert(4, supplyTypeCombo);
        }

        private void btnSearch1_ByIsCurrent(object sender, EventArgs e)
        {
            dgDeliveryPoints.DataSource = null
            dgDeliveryPoints.DataSource = dpResult;
            if (!dgDeliveryPoints.Columns.Contains(supplyTypeCombo))
                 CreateSupplyTypeColumn();
            else
                 supplyTypeCombo.DisplayIndex = 4;

            foreach (DataGridViewRow row in dgDeliveryPoints.Rows)
                row.Cells[4].Value = row.Cells["SupplyTypeID"].Value;

}
我第一次按下搜索按钮时,btnSearch1\u ByIsCurrent方法将触发,supplyTypeCombo看起来不错。它正确地定位在dgDeliveryPoints网格中,并且具有正确的值,但是当我再次按下搜索按钮时,会出现奇怪的行为。 当我再次按下按钮时,supplyTypeCombo列的索引从4变为3??为什么会发生这种情况?此外,我拥有此代码的原因:

        if (!dgDeliveryPoints.Columns.Contains(supplyTypeCombo))
            CreateSupplyTypeColumn();
        else
            supplyTypeCombo.DisplayIndex = 4;

这是因为,即使我每次单击按钮都会将数据源清空到dgDeliveryPoints,以重新设置所有内容,并尝试将supplyTypeCombo列添加回,我还是会收到一个异常,说明它已经存在。为什么会发生这种情况???

您是否尝试过更改这行代码:

row.Cells[4].Value = row.Cells["SupplyTypeID"].Value;
为此:

supplyTypeCombo.DataPropertyName = "SupplyTypeID";