简体   繁体   English

DataGridViewComboBoxColumn 事件触发次数过多

[英]DataGridViewComboBoxColumn Event Fire too many times

I am having problems stopping the event firing a second time after selecting aa value in the drop down combo.在下拉组合中选择 aa 值后,我在第二次停止事件触发时遇到问题。 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:我有一个网格视图,上面有 2 列:

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:当我在第一行的下拉列表中单击一个新值时,会按预期触发该事件: 在此处输入图片说明

When I click yes the dialog goes away but the dropdown still looks like it's selected as it appears different than the others:当我单击是时,对话框消失了,但下拉菜单仍然看起来像它被选中,因为它看起来与其他人不同: 在此处输入图片说明

As soon as I try to select an arrow of another dropdown, it is fired again like its not been unfocused.一旦我尝试选择另一个下拉列表的箭头,它就会再次被触发,就像它没有被聚焦一样。 在此处输入图片说明

How do I stop if being focused after I clicked yes during the first dialog box?如果在第一个对话框中单击“是”后集中注意力,如何停止?

I have found the solution.我找到了解决办法。 I am using both CellValueChanged and CurrentCellDirtyStateChanged to make it work.我同时使用 CellValueChanged 和 CurrentCellDirtyStateChanged 使其工作。 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)
            {

            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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