DataGridViewComboBoxColumn Event Fire too many times

137 Views Asked by At

I am having problems stopping the event firing a second time after selecting a a value in the drop down combo. The event opens a dialog which tells the user if they want to save it with a yes or no option.

I have a grid view has 2 columns on it:

My code is below.

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        List<Team> teamList = new List<Team>();
        List<Team_Colour> teamColourList = new List<Team_Colour>();
        DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            teamList.Add(new Team { name = "Test A", Colour_ID = 1});
            teamList.Add(new Team { name = "Test B", Colour_ID = 2 });
            teamList.Add(new Team { name = "Test C", Colour_ID = 3 });
            teamList.Add(new Team { name = "Test D", Colour_ID = 4 });

            dataGridView1.DataSource = teamList;
            dataGridView1.Columns[1].Visible = false;
            cb.HeaderText = "Colour";

            teamColourList.Add(new Team_Colour { Colour_ID = 1, Colour_Name = "None" });
            teamColourList.Add(new Team_Colour { Colour_ID = 2, Colour_Name = "Green" });
            teamColourList.Add(new Team_Colour { Colour_ID = 3, Colour_Name = "Blue" });
            teamColourList.Add(new Team_Colour { Colour_ID = 4, Colour_Name = "Red" });


            cb.DataSource = teamColourList;
            cb.DisplayMember = "Colour_Name";
            cb.ValueMember = "Colour_ID";
            cb.DataPropertyName = "Colour_ID";


            dataGridView1.Columns.Insert(1, cb);

        }

        private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure", "Saving", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                
            }
            else if (dialogResult == DialogResult.No)
            {
                
            }
        }
    }
}

When I click a new value in the drop down for the first row, the event is fired as expected: enter image description here

When I click yes the dialog goes away but the dropdown still looks like it's selected as it appears different than the others: enter image description here

As soon as I try to select an arrow of another dropdown, it is fired again like its not been unfocused. enter image description here

How do I stop if being focused after I clicked yes during the first dialog box?

1

There are 1 best solutions below

4
On

I have found the solution. I am using both CellValueChanged and CurrentCellDirtyStateChanged to make it work. My code is below.

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        List<Team> teamList = new List<Team>();
        List<Team_Colour> teamColourList = new List<Team_Colour>();
        DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            teamList.Add(new Team { name = "Test A", Colour_ID = 1});
            teamList.Add(new Team { name = "Test B", Colour_ID = 2 });
            teamList.Add(new Team { name = "Test C", Colour_ID = 3 });
            teamList.Add(new Team { name = "Test D", Colour_ID = 4 });

            dataGridView1.DataSource = teamList;
            dataGridView1.Columns[1].Visible = false;
            cb.HeaderText = "Colour";

            teamColourList.Add(new Team_Colour { Colour_ID = 1, Colour_Name = "None" });
            teamColourList.Add(new Team_Colour { Colour_ID = 2, Colour_Name = "Green" });
            teamColourList.Add(new Team_Colour { Colour_ID = 3, Colour_Name = "Blue" });
            teamColourList.Add(new Team_Colour { Colour_ID = 4, Colour_Name = "Red" });


            cb.DataSource = teamColourList;
            cb.DisplayMember = "Colour_Name";
            cb.ValueMember = "Colour_ID";
            cb.DataPropertyName = "Colour_ID";


            dataGridView1.Columns.Insert(1, cb);

        }

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

        private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

            DialogResult dialogResult = MessageBox.Show("Are you sure", "Saving", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {

            }
            else if (dialogResult == DialogResult.No)
            {

            }
        }
    }
}