简体   繁体   中英

c# datagrid combobox getvalue selected

I have a datagrid with a combobox, i want to get my value, i can get it but i dont know why, i get it 4 times ??? could someone help me ?

here my code :

    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);
            }

The messagebox appears 4 times when i choose a value in combobox. Thanks for your help

In dgvLocataire_CellMouseClick method, you are subscribing to dgvLocataire_CellValueChanged every time you click. Which means it can be called multiple times => MessageBox.Show(comboboxSelectedValue) is called multiple times.

You should subscribe to this event only once at the initialization of this form.

I change like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM