combox点击但没有更改选项但是触发selectedindexchange事件

164 阅读1分钟
        {
            for (int i = 0; i < dataGridView.RowCount; i++)
            {
                if (dataGridView.Rows[i].Cells.Count.Equals(0))
                {
                    return;
                }
            }

            this.mSelectedRowIndex = this.dataGridView.CurrentCell.RowIndex;
            DataGridViewRow row = this.dataGridView.Rows[mSelectedRowIndex];

            selectBefore = Convert.ToString(row.Cells[DataGridViewColumnIndexSections].Value);
            if (selectBefore.Equals(""))
            {
                return;
            }
            if (this.dataGridView.CurrentCell.ColumnIndex == DataGridViewColumnIndexSections)
            {
                ComboBox comboBox = e.Control as ComboBox;
                comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
                comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
            }
        }

通过dataGridView_EditingControlShowing来调用事件 dataGridView_EditingControlShowing:编辑单元格的时候调用,也就是说开始编辑单元格就调用了indexchange 然后如果选择的项和一开始不一样则继续事件,如果选择的是一样的则不继续。——问题是点击combox之后并没有进行选择,但是它这里传进来的选择的项改变了

        {
            if (this.mSelectedRowIndex >= 0)
            {
                ComboBox combox = sender as ComboBox;
                combox.Leave += new EventHandler(combox_Leave);
                try
                {
                    //取当前选中行的codename  移出的地址,移入的地址
                    DataGridViewRow row = this.dataGridView.Rows[mSelectedRowIndex];
                    string CurrentName = row.Cells[DataGridViewColumnIndexName].Value.ToString();
                    string FirstMemoryName = row.Cells[DataGridViewColumnIndexSections].Tag.ToString();
                    string FirstType = GetType(FirstMemoryName);

                    if (combox.SelectedIndex >= 0)
                    {
                        string FinalMemoryName = combox.SelectedItem.ToString();
                        if (FinalMemoryName.Equals(FirstMemoryName))
                        {
                            return;
                        }
                        row.Cells[DataGridViewColumnIndexSections].Tag = FinalMemoryName;
                        string FinalType = GetType(FinalMemoryName);
                        this.mLayoutInterface.ChangeSection(CurrentName, FirstMemoryName, FirstType, FinalMemoryName, FinalType);
                        int codeSectionIndex = (int)row.Tag;
                    }
                }
                catch (Exception ex)
                {
                    this.mLayoutInterface.HandleAlarm(AlarmLevel.Error, "Fail to switch code section: " + ex.Message);
                }
            }
        }

感觉是时间差造成了,可能需要用background,因为dataGridView_EditingControlShowing在点击下拉框的时候触发,然后直接触发comboBox_SelectedIndexChanged,但是因为连续点击很快,一个还没响应完,传进来的参数就变成另一个,对应不上上一个导致的问题,所以把取值那里不用实时事件触发的sender,而是用在dataGridView_EditingControlShowing获取的参数row,报纸值是对应的上的,然后就可以了

//string FinalMemoryName = combox.SelectedItem.ToString();
string FinalMemoryName = row.Cells[DataGridViewColumnIndexSections].EditedFormattedValue.ToString();