在“MouseClick”事件中在DataGridView中添加新行时出现问题

时间:2012-10-09 08:10:14

标签: c# events datagridview

我需要在“MouseClick”事件上向DataGridView添加一个新行。我这样做,但是当我点击现有单元格时也会添加该行。我在“CellClick / CellEndEdit”事件上有另一个处理程序,并且“MouseClick”也与它们共同提出。怎么避免这个?请帮助解决这个问题。

感谢。

CellEndEdit事件:

private void dataGridViewWorks_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        FormSelectItem frm = new FormSelectItem(formMain);
        switch (dataGridViewWorks.Columns[e.ColumnIndex].Name)
        {
            case "WorksWORKNAME":
                //some functions
                break;

            case "WorksCATNR":
                //some functions
                break;

            case "WorksQuantity":

                //some functions
                break;
            default:
                break;
        }
    }

MousClick事件:

private void dataGridViewWorks_MouseClick(object sender, MouseEventArgs e)
    {
        if (dataGridViewWorks.Rows.Count == 0)
        {
            dataGridViewWorks.Rows.Add();
            return;
        }
        dataGridViewWorks.Rows.Insert(dataGridViewWorks.Rows.Count);
        dataGridViewWorks.CurrentCell = dataGridViewWorks.Rows[dataGridViewWorks.Rows.Count - 1].Cells[1];
    }

1 个答案:

答案 0 :(得分:1)

代替dataGridViewWorks_MouseClick,您可以使用dataGridViewWorks_MouseDoubleClick添加行,这是一个好主意:)