DataGridViewComboBoxColumn - delay values ​​for a split second

I have a DataGridView with multiple DataGridViewComboBoxColumns. The DataGridView has a CellEnter event handler to remove drop-down lists once.

The column is bound to the KeyValuePairs list, ValueMember to "Key", and DisplayMember to "Value".

When I click on the combobox column, it works fine. However, if the cell is in the “drop-down list” state and I click on another combo box (the same column, another row), it will properly deselect the old cell, select and omit the new cell, however, the selected value from the top changes to the value from the old cell for a second of a second before returning to the correct one.

For example, let's say that list A, B, C. In line 1, A is selected, in line 2, B. is selected. I click on the cell in line1, everything is as it should be. Then, while this cell fell, I click on the cell in row2. It falls correctly, but the selected value on top becomes A, and then immediately switches to B (correct).

If I click on a cell in some other column before clicking on the second cell with a list, this will not happen.

Is there any way to prevent this?

Sample code to reproduce the problem (event handlers are connected to obvious events):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PDGV
{
    public partial class Form1 : Form
    {
        List<KeyValuePair<string, string>> bindingList = new List<KeyValuePair<string, string>>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(10);
            bindingList.Add(new KeyValuePair<string,string>("aaa", "111"));
            bindingList.Add(new KeyValuePair<string,string>("bbb", "222"));
            bindingList.Add(new KeyValuePair<string,string>("ccc", "333"));
            bindingList.Add(new KeyValuePair<string,string>("ddd", "444"));
            bindingList.Add(new KeyValuePair<string,string>("eee", "555"));
            BindComboList(2, bindingList);

        }

        private void BindComboList(int columnIndex, object list)
        {
            var column = dataGridView1.Columns[columnIndex] as DataGridViewComboBoxColumn;
            if (column != null)
            {
                column.DataSource = new BindingSource(list, null);
                column.DisplayMember = "Value";
                column.ValueMember = "Key";
            }
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            dataGridView1.BeginEdit(true);
            var control = dataGridView1.EditingControl as DataGridViewComboBoxEditingControl;
            if (control != null)
                control.DroppedDown = true;
        }
    }
}
+5
source share
3 answers

Cuase:

DataGridView EndEdit EditingControl this.latestEditingControl = this.editingControl; , , BeginEditInternal. , lastEditingControl , , , , , .

:

lastEditingControl null, /, . . , , , :), , .

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1)
            return;

        //this.latestEditingControl
        Type t = dataGridView1.GetType();
        FieldInfo viewSetter = t.GetField("latestEditingControl", BindingFlags.Default | BindingFlags.NonPublic | BindingFlags.Instance);
        viewSetter.SetValue(dataGridView1, null);
    }
+4

100% , ( ), CellEnter ( DataGridView ?)

public Form1() {
  InitializeComponent();

  dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
  dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
  if (e.Control is ComboBox) {
    SendKeys.Send("{F4}");
  }
}

ComboBox, .


( )

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
  if (e.Control is ComboBox) {
    ComboBox ctl = e.Control as ComboBox;
    ctl.Enter -= new EventHandler(ctl_Enter);
    ctl.Enter += new EventHandler(ctl_Enter);
  }
}

void ctl_Enter(object sender, EventArgs e) {
  (sender as ComboBox).DroppedDown = true;
}
+2

, 3D- 7 (, Vista , )

, ,

comboboxcolumn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

, , .

comboboxcolumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

, ( "" ) combobox. , . , - combox , , , , .

Try switching to the "classic" Windows theme. Squad must behave then. It would be interesting if you could get the combobox to display as it does under the classic theme while in Aero. I'm not sure if this is possible, but you can look into it.

But anyway, the fact that dgv does not have this problem with other combobox styles makes me think that this new combobox style doesn’t work well with datagridview.

+1
source

All Articles