0

我有一个带有组合框的数据网格,我想获得我的价值,我可以得到它,但我不知道为什么,我得到了 4 次???有人可以帮助我吗?

这是我的代码:

    private void dgvLocataire_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                dgvLocataire.BeginEdit(false);

                var ec = dgvLocataire.EditingControl as DataGridViewComboBoxEditingControl;
                if (ec != null && ec.Width - e.X < SystemInformation.VerticalScrollBarWidth)
                    ec.DroppedDown = true;

                if ((e.ColumnIndex != 3) && (e.ColumnIndex != 4))
                {
                    dgvLocataire.Columns[e.ColumnIndex].ReadOnly = true;
                }

                dgvLocataire.CellValueChanged +=
                 new DataGridViewCellEventHandler(dgvLocataire_CellValueChanged);
                //dgvLocataire.CurrentCellDirtyStateChanged +=
                             //new EventHandler(dgvLocataire_CurrentCellDirtyStateChanged);

            }

    private void dgvLocataire_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                dgvLocataire.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }


    private void dgvLocataire_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                string comboboxSelectedValue = string.Empty;

                if (dgvLocataire.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewComboBoxColumn))
                    comboboxSelectedValue = dgvLocataire.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                MessageBox.Show(comboboxSelectedValue);
            }

当我在组合框中选择一个值时,消息框出现 4 次。谢谢你的帮助

4

2 回答 2

0

dgvLocataire_CellMouseClick方法中,您每次单击时都订阅dgvLocataire_CellValueChanged 。这意味着它可以被多次调用 => MessageBox.Show(comboboxSelectedValue) 被多次调用。

您应该只在此表单初始化时订阅此事件一次。

于 2019-12-06T13:33:40.453 回答
0

我这样改变

private void dgvLocataire_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dgvLocataire.BeginEdit(false);


            var ec = dgvLocataire.EditingControl as DataGridViewComboBoxEditingControl;
            if (ec != null && ec.Width - e.X < SystemInformation.VerticalScrollBarWidth)
                ec.DroppedDown = true;

            if ((e.ColumnIndex != 3) && (e.ColumnIndex != 4))
            {
                dgvLocataire.Columns[e.ColumnIndex].ReadOnly = true;
            }

        }

private void dgvLocataire_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                cb.SelectionChangeCommitted -= new EventHandler(ComboBox_SelectedIndexChanged);

                // now attach the event handler
                cb.SelectionChangeCommitted += new EventHandler(ComboBox_SelectedIndexChanged);
            }
        }

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            string item = cb.Text;
            if (item != null)
                MessageBox.Show(item);
        }
`
but now i dont get the messagebox to show ???
any help ?
Thanks
于 2019-12-06T13:57:04.687 回答