停止SelectedValueChanged事件

时间:2015-02-12 01:58:35

标签: c# events handler

我只想问一下如何在我的代码中停止选定的值更改事件。我有SelectedValueChanged事件和SelectedIndexChanged事件。我正在使用SelectedIndexChanged事件,因为要在gridview上找到任何重复内容供您理解,这是我的代码。

SelectedValueChanged代码:

private void cmbField_SelectedValueChanged(object sender, EventArgs e)
    {
        DataGridViewRow GridRowLoc = this.dgvFilter.CurrentRow;
        AddGrid(iRowIdx);
        int iRowCount = this.dgvFilter.RowCount - 1;

        if (this.cmbField.Text != "System.Data.DataRowView")
        {
            this.dgvFilter.Rows[iRowIdx].Cells["ColumnFieldName"].Value = this.cmbField.Text;
            this.dgvFilter.Rows[iRowIdx].Cells["FieldName"].Value = this.cmbField.SelectedValue;

            if (iRowCount <= iRowIdx)
            {
                DataRow drow = dttable.NewRow();
                drow["ColumnNames"] = this.cmbField.Text;
                drow["FieldName"] = this.cmbField.SelectedValue;
                drow["Alias"] = string.Empty;
                drow["DataType"] = string.Empty;
                drow["Outputs"] = false;
                drow["SortType"] = string.Empty;
                drow["SortOrder"] = string.Empty;
                drow["GroupBy"] = string.Empty;
                drow["Filter"] = string.Empty;
                drow["Or1"] = string.Empty;
                drow["Or2"] = string.Empty;
                drow["Or3"] = string.Empty;
                drow["Or4"] = string.Empty;
                drow["Or5"] = string.Empty;
                drow["Or6"] = string.Empty;
                drow["Or7"] = string.Empty;
                drow["Or8"] = string.Empty;
                drow["Or9"] = string.Empty;
                drow["Or10"] = string.Empty;
                dttable.Rows.Add(drow);
            }
            else
            {
                int irow = 0;
                foreach (DataRow dr in dttable.Rows)
                {
                    if (irow == iRowIdx)
                    {
                        dr["ColumnNames"] = this.cmbField.Text;
                        dr["FieldName"] = this.cmbField.SelectedValue;
                    }
                    irow++;
                }
            }

            CheckAlias(iRowIdx, this.cmbField.Text, dgvFilter);
            checkcellvalue(this.cmbField.Text, iRowIdx);
            CheckSorting();
            if (bGroupBySelected == true)
            {
                this.dgvFilter.Rows[iRowIdx].Cells["GroupBy"].Value = "Group By";
            }

            this.dgvFilter.DataSource = dttable;
            dsFilter.AcceptChanges();

            this.cmbField.Visible = false;
        }
        //checkcellvalue(this.cmbField.Text, iRowIdx);
        //MessageBox.Show(arr_Filter[0]);

        CheckoutputEnable();
    }

然后这是我的SelectedIndexChanged代码:

private void cmbField_SelectedIndexChanged(object sender, EventArgs e)
    {            
        try
        {
            if (dgvFilter.Rows.Count > 1)
            {
                int count = this.dgvFilter.Rows.Count;

                DataRowView oDataRowView = cmbField.SelectedItem as DataRowView;
                string sValue = string.Empty;

                if (oDataRowView != null)
                {
                    sValue = oDataRowView.Row["FieldDescription"] as string;
                }

                for (int j = 0; j < count; j++)
                {
                    sValue = this.cmbField.Text;
                    if ((j + 2) != dgvFilter.Rows.Count)
                    {
                        if ((sValue == this.dgvFilter.Rows[j].Cells["ColumnFieldName"].Value.ToString()))
                        {
                            if (this.dgvFilter.Rows.Count > 2)
                            {
                                MessageBox.Show("Field already in the list");
                                DataGridViewRow GridRowLoc2 = this.dgvFilter.CurrentRow;
                                this.dgvFilter.Rows.Remove(GridRowLoc2);

                                break;
                            }
                        }

                    }

                }
            }
        }

        catch (Exception ex)
        {

        }

    }

加载表单时我的代码就是你们需要的。

private void FormAdhocReportViewer_Load(object sender, EventArgs e)
    {
        try
        {
            //string sample = "";
            sXMLResult = SQLScript;
            AddGrid(0);
            adhoc.RowID = CurrentUserNameRowID;
            adhoc.FieldGroup = toolStripReportGroup.Text;
            XMLDOC = adhoc.get_sp_Get_Field_Settings();


            if (RowID == "0")
            {
               // XMLDOC = adhoc.get_sp_Get_Field_Settings();
                LoadDataSet(XMLFILTER);
            }
            else
            {
               // XMLDOC = sXMLFieldSettings;
                XMLFieldQuery = XMLFieldQuery.Replace("@", "<").Replace("+", ">").Replace("*", "/");                   
                LoadDataSet(XMLFieldQuery);
            }
            LoadComboField();
        }
        catch { }
    }

LoadComboField用于组合框中的项目,代码是这个

private void LoadComboField()
    {
       ReadXMLData(XMLDOC, dsCombo);
       dt = dsCombo.Tables[0];
       DataView dv1 = new DataView(dsCombo.Tables[0]);
       this.cmbField.Items.Clear();
       this.cmbField.DataSource = dv1;
       this.cmbField.DisplayMember = "FieldDescription";
       this.cmbField.ValueMember = "FieldName";
    }

每次代码 DataGridViewRow GridRowLoc2 = this.dgvFilter.CurrentRow; this.dgvFilter.Rows.Remove(GridRowLoc2); 执行SelectedValueChanged执行的事件..我只想停止SelectedValueChangedEvent它很难解释,因为我的代码太复杂了。

1 个答案:

答案 0 :(得分:0)

如何简单地删除事件处理程序?

只是做:

if (<whatever>)
{
   this.cmbField.SelectedValueChanged -= cmbField_SelectedValueChanged;
}
豫ICP备18024241号-1