C# 当它为DataGridView C工作时,CellEndEdit#

C# 当它为DataGridView C工作时,CellEndEdit#,c#,C#,有人能帮助我理解为什么数据网格视图中的事件不总是以相同的方式工作吗 例如 CellEndEdit有时在按enter键时工作,有时根本不调用CellEndEdit 还有一次,当我编写DataGridView.Endedit()时;它调用了事件CellEndEdit,我看到因为我调试了执行,第二天我也使用debug检查了代码,但根本没有调用,这对我来说很奇怪 CellEndEdit在使用鼠标或行更改CurrentCell时起作用,在为当前单元格更改DataGridView的只读属性时也起作用 我现在

有人能帮助我理解为什么数据网格视图中的事件不总是以相同的方式工作吗

例如

CellEndEdit有时在按enter键时工作,有时根本不调用CellEndEdit

还有一次,当我编写DataGridView.Endedit()时;它调用了事件CellEndEdit,我看到因为我调试了执行,第二天我也使用debug检查了代码,但根本没有调用,这对我来说很奇怪

CellEndEdit在使用鼠标或行更改CurrentCell时起作用,在为当前单元格更改DataGridView的只读属性时也起作用 我现在很困惑,不能再思考了,因为什么看起来很好,工作得很好,明天在DataGridView和它的活动中以另一种方式工作

我有这个代码完美地工作时,按回车键或使用箭头结束编辑单元

private bool AllRowCellsEntered(DataGridView dataGridView, int row)
    {
        bool allEntered = true;
        for (int i = 1; i < dataGridView.Columns.Count; i++)
        {
            if (dataGridView.Rows[row].Cells[i].Value != null)
            {
                int result;
                if (!(int.TryParse(dataGridView.Rows[row].Cells[i].Value.ToString(), out result)))
                {
                    allEntered = false;
                    // dataGridView.Rows[row].Cells[i].Value = "";
                    break;
                }
            }
            else
            {
                allEntered = false;
                break;
            }
        }
        return allEntered;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.Rows.Add();
        dataGridView1[0, 0].Value = 1;
        dataGridView1[0, 0].ReadOnly = true;
        dataGridView1.CurrentCell = dataGridView1[1, 0];

    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if ((dataGridView1.CurrentCell != null))
        {
            if ((dataGridView1.CurrentCell.Value != null))
            {
                string _cellValue = dataGridView1.CurrentCell.Value.ToString();
                int _parsedCellValue = 0;
                {
                    if (!(Int32.TryParse(_cellValue, out _parsedCellValue)))
                    {
                        dataGridView1.CurrentCell.Value = null;
                    }
                    else
                    {
                        KeyEventArgs enterKeyDown = new KeyEventArgs(Keys.Enter);
                        dataGridView1_KeyDown(dataGridView1, enterKeyDown);
                    }
                }
            }
        }
        if (dataGridView1.Rows.Count == 10)
        {
            if (AllRowCellsEntered(dataGridView1, dataGridView1.Rows.Count - 1))
            {
                dataGridView1.ReadOnly = true;
                dataGridView1.CurrentCell = null;
                MessageBox.Show("done!");
            }
        }

    }
    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        int col = dataGridView1.CurrentCell.ColumnIndex;
        int row = dataGridView1.CurrentCell.RowIndex;

        if (e.KeyCode == Keys.Enter)
        {
            if (!(dataGridView1.RowCount == 10 && AllRowCellsEntered(dataGridView1, 9)))
            {
                bool allEntered = true;

                allEntered = AllRowCellsEntered(dataGridView1, row);
                if (allEntered)
                {
                    if (row == dataGridView1.Rows.Count - 1)
                    {
                        dataGridView1.Rows.Add(1);
                        dataGridView1.Rows[row].Cells[0].Value = row + 1;
                        dataGridView1.Rows[row + 1].Cells[0].Value = row + 2;
                        dataGridView1.Rows[row].ReadOnly = true;
                        dataGridView1[0, row + 1].ReadOnly = true;
                        dataGridView1.CurrentCell = dataGridView1.Rows[row + 1].Cells[1];
                    }
                    else
                    {
                        dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.Rows.Count - 1];
                    }

                }
                else if (col == dataGridView1.Columns.Count - 1)
                {
                    MessageBox.Show("you are in the last cell and not all values are entered,\n enter all values please!");
                }
                else
                {
                    dataGridView1.CurrentCell = dataGridView1[col + 1, row];
                }
            }
        }

   }
private bool allrowcellsenter(DataGridView DataGridView,int行)
{
bool allEntered=true;
for(int i=1;i
但是,当我使用鼠标在同一行的单元格之间移动时,出现了一个问题。 所以我试着这样解决这个问题

   protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            dataGridView1.EndEdit();// if i did not use it did not read the value of cuurent cell when calling AllRowCellsEntered method, here did not call the event dataGridView1_CellEndEdit may be because the property EditMode is EditOnEnter for DGV! 

            if (dataGridView1 != null &&
                dataGridView1.SelectedCells != null &&
                dataGridView1.CurrentCell != null)
            {

                if (!(dataGridView1.RowCount == 10 && AllRowCellsEntered(dataGridView1, 9)))
                {
                    bool allEntered = true;
                    int col = dataGridView1.CurrentCell.ColumnIndex;
                    int row = dataGridView1.CurrentCell.RowIndex;
                    //dataGridView1.EndEdit();
                    //check if all values in the current row are entered
                    //string ss = dataGridView1.CurrentCell.Value.ToString();//here  error if the cell value is null
                    allEntered = AllRowCellsEntered(dataGridView1, row);
                    if (allEntered)
                    {
                        if (row == dataGridView1.Rows.Count - 1)
                        {
                            dataGridView1.Rows.Add(1);
                            dataGridView1.Rows[row].Cells[0].Value = row + 1;
                            dataGridView1.Rows[row + 1].Cells[0].Value = row + 2;
                            dataGridView1.Rows[row].ReadOnly = true;
                            dataGridView1[0, row + 1].ReadOnly = true;
                            dataGridView1.CurrentCell = dataGridView1.Rows[row + 1].Cells[1];
                            return true;
                        }
                        else
                        {
                            dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.Rows.Count - 1];
                            return base.ProcessCmdKey(ref msg, keyData);
                        }

                    }
                    else if (col == dataGridView1.Columns.Count - 1)
                    {
                        MessageBox.Show("you are in the last cell and not all values are entered,\n enter all values please!");
                    }
                    else
                    {
                        dataGridView1.CurrentCell = dataGridView1[col + 1, row];
                    }
                }
                else
                {
                    dataGridView1.ReadOnly = true;
                }
            }

        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
    private bool AllRowCellsEntered(DataGridView dataGridView, int row)
    {
        bool allEntered = true;
        for (int i = 1; i < dataGridView.Columns.Count; i++)
        {
            if (dataGridView.Rows[row].Cells[i].Value != null)
            {
                int result;
                if (!(int.TryParse(dataGridView.Rows[row].Cells[i].Value.ToString(), out result)))
                {
                    allEntered = false;
                    // dataGridView.Rows[row].Cells[i].Value = "";
                    break;
                }
            }
            else
            {
                allEntered = false;
                break;
            }
        }
        return allEntered;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.Rows.Add();
        dataGridView1[0, 0].Value = 1;
        dataGridView1[0, 0].ReadOnly = true;
        dataGridView1.CurrentCell = dataGridView1[1, 0];

    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {

        if ((dataGridView1.CurrentCell != null))
        {
            if ((dataGridView1.CurrentCell.Value != null))
            {
                string _cellValue = dataGridView1.CurrentCell.Value.ToString();
                int _parsedCellValue = 0;
                {
                    if (!(Int32.TryParse(_cellValue, out _parsedCellValue)))
                    {
                        dataGridView1.CurrentCell.Value = null;
                    }
                }
            }
        }
        if (dataGridView1.Rows.Count == 10)
        {
            if (AllRowCellsEntered(dataGridView1, dataGridView1.Rows.Count - 1))
            {
                dataGridView1.ReadOnly = true;
                dataGridView1.CurrentCell = null;// can move it but
                //dataGridView1.Enabled = false;
                MessageBox.Show("done!");
            }
        }

    }
protectedoverride bool ProcessCmdKey(参考消息msg,Keys keyData)
{
if(keyData==Keys.Enter)
{
dataGridView1.EndEdit();//如果我没有使用它,在调用AllRowCellsEntered方法时没有读取cuurent单元格的值,此处没有调用事件dataGridView1_CellEndEdit可能是因为属性EditMode是DGV的Editonneter!
如果(dataGridView1!=null&&
dataGridView1.SelectedCells!=null&&
dataGridView1.CurrentCell!=null)
{
如果(!(dataGridView1.RowCount==10&&AllRowCellSenter(dataGridView1,9)))
{
bool allEntered=true;
int col=dataGridView1.CurrentCell.ColumnIndex;
int row=dataGridView1.CurrentCell.RowIndex;
//dataGridView1.EndEdit();
//检查是否输入了当前行中的所有值
//字符串ss=dataGridView1.CurrentCell.Value.ToString();//如果单元格值为null,则此处为错误
allEntered=AllRowCellsEntered(dataGridView1,行);
如果(allEntered)
{
如果(行==dataGridView1.R
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {

        if (keyData == Keys.Enter)
        {
           // dataGridView1.EndEdit();

            if (dataGridView1 != null &&
                dataGridView1.SelectedCells != null &&
                dataGridView1.CurrentCell != null)
               // && !dataGridView1.ReadOnly)
            {
                dataGridView1.EndEdit();
                //dataGridView1.CurrentCell.ReadOnly = true;
                //dataGridView1.CurrentCell.ReadOnly = false;

                if (!(dataGridView1.RowCount == 10 && AllRowCellsEntered(dataGridView1, 9)))
                {

                    bool allEntered = true;
                    int col = dataGridView1.CurrentCell.ColumnIndex;
                    int row = dataGridView1.CurrentCell.RowIndex;
                    //dataGridView1.EndEdit();
                    //check if all values in the current row are entered
                    allEntered = AllRowCellsEntered(dataGridView1, row);
                    if (allEntered)
                    {
                        if (row == dataGridView1.Rows.Count - 1)
                        {
                            dataGridView1.Rows.Add(1);
                            dataGridView1.Rows[row].Cells[0].Value = row + 1;
                            dataGridView1.Rows[row + 1].Cells[0].Value = row + 2;
                            dataGridView1.Rows[row].ReadOnly = true;
                            dataGridView1[0, row + 1].ReadOnly = true;
                            dataGridView1.CurrentCell = dataGridView1.Rows[row + 1].Cells[1];
                            return true;
                        }
                        else
                        {
                            dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.Rows.Count - 1];
                            return base.ProcessCmdKey(ref msg, keyData);
                        }

                    }
                    else if (col == dataGridView1.Columns.Count - 1)
                    {
                        MessageBox.Show("you are in the last cell and not all values are entered,\n enter all values please!");
                    }
                    else
                    {
                        dataGridView1.CurrentCell = dataGridView1[col + 1, row];
                    }
                }
                else

                {
                    //dataGridView1.CurrentCell=null;
                }
            }

        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
    private bool AllRowCellsEntered(DataGridView dataGridView, int row)
    {
        bool allEntered = true;
        for (int i = 1; i < dataGridView.Columns.Count; i++)
        {
            if (dataGridView.Rows[row].Cells[i].Value != null)
            {
                int result;
                if (!(int.TryParse(dataGridView.Rows[row].Cells[i].Value.ToString(), out result)))
                {
                    allEntered = false;
                    // dataGridView.Rows[row].Cells[i].Value = "";
                    break;
                }
            }
            else
            {
                allEntered = false;
                break;
            }
        }
        return allEntered;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.Rows.Add();
        dataGridView1[0, 0].Value = 1;
        dataGridView1[0, 0].ReadOnly = true;
        dataGridView1.CurrentCell = dataGridView1[1, 0];

    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {

        if ((dataGridView1.CurrentCell != null))
        {
            if ((dataGridView1.CurrentCell.Value != null))
            {
                string _cellValue = dataGridView1.CurrentCell.Value.ToString();
                int _parsedCellValue = 0;
                {
                    if (!(Int32.TryParse(_cellValue, out _parsedCellValue)))
                    {
                        dataGridView1.CurrentCell.Value = null;
                    }
                }
            }
        }
        if (dataGridView1.Rows.Count == 10)
        {
            if (AllRowCellsEntered(dataGridView1, dataGridView1.Rows.Count - 1))
            {
                dataGridView1.ReadOnly = true;
                dataGridView1.CurrentCell = null;
                MessageBox.Show("done!");
            }
        }

    }
        private bool AllRowCellsEntered(DataGridView dataGridView, int row)
    {
        bool allEntered = true;
        for (int i = 1; i < dataGridView.Columns.Count; i++)
        {
            if (dataGridView.Rows[row].Cells[i].Value != null)
            {
                int result;
                if (!(int.TryParse(dataGridView.Rows[row].Cells[i].Value.ToString(), out result)))
                {
                    allEntered = false;
                    // dataGridView.Rows[row].Cells[i].Value = "";
                    break;
                }
            }
            else
            {
                allEntered = false;
                break;
            }
        }
        return allEntered;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.Rows.Add();
        dataGridView1[0, 0].Value = 1;
        dataGridView1[0, 0].ReadOnly = true;
        dataGridView1.CurrentCell = dataGridView1[1, 0];

    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if ((dataGridView1.CurrentCell != null))
        {
            if ((dataGridView1.CurrentCell.Value != null))
            {
                string _cellValue = dataGridView1.CurrentCell.Value.ToString();
                int _parsedCellValue = 0;
                {
                    if (!(Int32.TryParse(_cellValue, out _parsedCellValue)))
                    {
                        dataGridView1.CurrentCell.Value = null;
                    }
                }
            }
        }
        if (dataGridView1.Rows.Count == 10)
        {
            if (AllRowCellsEntered(dataGridView1, dataGridView1.Rows.Count - 1))
            {
                dataGridView1.ReadOnly = true;
                //dataGridView1.CurrentCell = null;
                MessageBox.Show("done!");

            }
        }

    }
    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        {

            if (e.KeyCode == Keys.Enter)
            {
                int col = dataGridView1.CurrentCell.ColumnIndex;
                int row = dataGridView1.CurrentCell.RowIndex;
                if (!(dataGridView1.RowCount == 10 && AllRowCellsEntered(dataGridView1, 9)))
                {
                    bool allEntered = true;

                    allEntered = AllRowCellsEntered(dataGridView1, row);
                    if (allEntered)
                    {
                        if (row == dataGridView1.Rows.Count - 1)
                        {
                            dataGridView1.Rows.Add(1);
                            dataGridView1.Rows[row].Cells[0].Value = row + 1;
                            dataGridView1.Rows[row + 1].Cells[0].Value = row + 2;
                            dataGridView1.Rows[row].ReadOnly = true;
                            dataGridView1[0, row + 1].ReadOnly = true;
                            dataGridView1.CurrentCell = dataGridView1.Rows[row + 1].Cells[1];
                        }
                        else
                        {
                            dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.Rows.Count - 1];
                        }

                    }
                    else if (col == dataGridView1.Columns.Count - 1)
                    {
                        MessageBox.Show("you are in the last cell and not all values are entered,\n enter all values please!");
                    }
                    else
                    {
                        dataGridView1.CurrentCell = dataGridView1[col + 1, row];
                    }
                }
            }
        }
   }
        bool _enter = false;
    private bool AllRowCellsEntered(DataGridView dataGridView, int row)
    {
        bool allEntered = true;
        for (int i = 1; i < dataGridView.Columns.Count; i++)
        {
            if (dataGridView.Rows[row].Cells[i].Value != null)
            {
                int result;
                if (!(int.TryParse(dataGridView.Rows[row].Cells[i].Value.ToString(), out result)))
                {
                    allEntered = false;
                    // dataGridView.Rows[row].Cells[i].Value = "";
                    break;
                }
            }
            else
            {
                allEntered = false;
                break;
            }
        }
        return allEntered;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.Rows.Add();
        dataGridView1[0, 0].Value = 1;
        dataGridView1[0, 0].ReadOnly = true;
        dataGridView1.CurrentCell = dataGridView1[1, 0];

    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if ((dataGridView1.CurrentCell != null))
        {
            if ((dataGridView1.CurrentCell.Value != null))
            {
                string _cellValue = dataGridView1.CurrentCell.Value.ToString();
                int _parsedCellValue = 0;
                {
                    if (!(Int32.TryParse(_cellValue, out _parsedCellValue)))
                    {
                        dataGridView1.CurrentCell.Value = null;
                    }
                    //else
                    //{
                    //    KeyEventArgs enterKeyDown = new KeyEventArgs(Keys.Enter);
                    //    //dataGridView1.EndEdit();

                    //    dataGridView1_KeyDown(dataGridView1, enterKeyDown);
                    //}
                    if (_enter)
                    {
                        KeyEventArgs enterKeyDown = new KeyEventArgs(Keys.Enter);
                        dataGridView1_KeyDown(dataGridView1, enterKeyDown);
                    }
                }
            }
        }
        if (dataGridView1.Rows.Count == 10)
        {
            if (AllRowCellsEntered(dataGridView1, dataGridView1.Rows.Count - 1))
            {
                dataGridView1.ReadOnly = true;
                //dataGridView1.CurrentCell = null;
                MessageBox.Show("done!");

            }
        }

    }
    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        //if (dataGridView1.CurrentCell != null)
        {

            if (e.KeyCode == Keys.Enter)
            {
                int col = dataGridView1.CurrentCell.ColumnIndex;
                int row = dataGridView1.CurrentCell.RowIndex;
                if (!(dataGridView1.RowCount == 10 && AllRowCellsEntered(dataGridView1, 9)))
                {
                    bool allEntered = true;

                    allEntered = AllRowCellsEntered(dataGridView1, row);
                    if (allEntered)
                    {
                        if (row == dataGridView1.Rows.Count - 1)
                        {
                            dataGridView1.Rows.Add(1);
                            dataGridView1.Rows[row].Cells[0].Value = row + 1;
                            dataGridView1.Rows[row + 1].Cells[0].Value = row + 2;
                            dataGridView1.Rows[row].ReadOnly = true;
                            dataGridView1[0, row + 1].ReadOnly = true;
                            dataGridView1.CurrentCell = dataGridView1.Rows[row + 1].Cells[1];
                        }
                        else
                        {
                            dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.Rows.Count - 1];
                        }

                    }
                    else if (col == dataGridView1.Columns.Count - 1)
                    {
                        MessageBox.Show("you are in the last cell and not all values are entered,\n enter all values please!");
                    }
                    else
                    {
                        dataGridView1.CurrentCell = dataGridView1[col + 1, row];
                    }
                }
                _enter = false;
            }
        }
   }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (!dataGridView1.ReadOnly)
        {
            if (keyData == Keys.Enter)
            {
                if (dataGridView1 != null &&
                    dataGridView1.SelectedCells != null &&
                    dataGridView1.CurrentCell != null)
                {
                    {
                        _enter = true;
                    }
                }
            }
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }