返回后为什么代码仍然在运行

时间:2016-10-25 19:14:57

标签: c# winforms datagridview event-handling

我在CellValueChanged datagridview事件中有一些代码,完整代码如下:

private void RationFormulationdgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (FirstShown == true)
    {
        return;
    }

    // I write this part to avoid inserting wrong character like 
    //negative number or letters but the problem is when user inter
    //negative number the `messageBox` will pup up but the coeds after
    //that still runs and insert wrong numbers into my database.
    //and this messageBox 
    //> MessageBox.Show("ErrorCellValueChangedEndCatch"); 
    //will pup up at the end.

    if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && PreValue != null)
    {
        bool IsCorrect = true;
        string Value = RationFormulationdgv[e.ColumnIndex, 
        e.RowIndex].Value.ToString();

        if (Value == string.Empty)
        {
            MessageBox.Show("Please Insert a Number!";

            IsCorrect = false;
        }
        else
        {
            try
            {
                Double CellValue = Double.Parse(Value);

                if (CellValue < 0)
                {
                    MessageBox.Show("Please use ONLY positive Number";

                    //@media when in debug I step into this line it goes
                    //on first line of event, and coeds are executing 
                    //from the first
                    RationFormulationdgv.Rows[e.RowIndex].
                    Cells[e.ColumnIndex].Value = PreValue;

                    IsCorrect = false;

                    // my point in this return doesn't work
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please insert ONLY Numbers");

                IsCorrect = false;

                RationFormulationdgv.Rows[e.RowIndex].
                Cells[e.ColumnIndex].Value = PreValue;

                return;
            }
        }

        if (!IsCorrect)
        {
            RationFormulationdgv.Rows[e.RowIndex].
            Cells[e.ColumnIndex].Value = PreValue;

            RationFormulationdgv.CurrentCell.Selected = false;

            return;
        }

        PreValue = null;
    }

    try
    {
        RationFormulationDBConnection.UpdateFeedsDetails(RationFormulationdgv);

        RationFormulationDBConnection.
        SetFeedsIntoRationFormulationdgv
        (RationFormulationdgv, RationTotaldgv);

        RationFormulationDBConnection.SetRationTotaldgv(RationTotaldgv);
    }
    catch
    {
        MessageBox.Show("ErrorCellValueChangedEndCatch");
    }
}

2 个答案:

答案 0 :(得分:2)

首次CellBeginEdit事件必须如下:

object PreValue = null;
bool StopAction = false;
private void RationFormulationdgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    PreValue = RationFormulationdgv[e.ColumnIndex, e.RowIndex].Value;

    try
    {
        if (Convert.ToDouble(PreValue) >= 0)
        {
           StopAction = false;
        }
    }
    catch (Exception)
    {
        MessageBox.Show("ErrorInSetingStopAction");
    }
}

然后CellValueChnaged事件必须是这样的:

private void RationFormulationdgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (FirstShown == true)
    {
        return;
    }

    if (PreValue != null && StopAction==false)
    {
        string Value = RationFormulationdgv[e.ColumnIndex, e.RowIndex].Value.ToString();

        if (Value == string.Empty)
        {
            MessageBox.Show("Please Insert a Number!");

            StopAction = true;
            RationFormulationdgv.Rows[e.RowIndex].
            Cells[e.ColumnIndex].Value = PreValue;   
        }
        else
        {
            try
            {
                Double CellValue = Double.Parse(Value);

                if (CellValue < 0)
                {
                    MessageBox.Show("Please use ONLY positive Number");

                    StopAction = true;
                    RationFormulationdgv.Rows[e.RowIndex].
                    Cells[e.ColumnIndex].Value = PreValue;

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please insert ONLY Numbers");

                StopAction = true;
                RationFormulationdgv.Rows[e.RowIndex].
                Cells[e.ColumnIndex].Value = PreValue;
            }
        }
    }

    if (StopAction)
    {
        return;
    }

    try
    {
        RationFormulationDBConnection RFDBC = new
        RationFormulationDBConnection();

        RFDBC.UpdateFeedsDetails(RationFormulationdgv);

        RFDBC.SetFeedsIntoRationFormulationdgv
        (RationFormulationdgv, RationTotaldgv);
        RFDBC.SetRationTotaldgv(RationTotaldgv);
    }
    catch
    {
        MessageBox.Show("ErrorCellValueChangedEndCatch");
    }

答案 1 :(得分:1)

实际上你的代码不完整但应该有明显的理由。面对return;后,必须终止该方法。我建议您使用以下代码并使用断点来查看FirstShown是真还是假:

private void RationFormulationdgv_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
    if (FirstShown == true)
    {
        return;
    }
    else if (FirstShown == false)
    {
        //The code that have to be run
    }
}


编辑:
看这里:

RationFormulationdgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = PreValue;

在程序的这一部分中,您正在更改网格单元格的值。因此,在更改网格值后立即RationFormulationdgv_CellValueChanged,当网格单元格的值发生更改时,会触发Leave。因此,只要当前执行的程序行到达上述更改单元格值行,就会再次执行该事件。另一种方法是使用>>> import numpy as np >>> arr = np.array([[0, 0, 0], [1, 1, 2], [1, 3, 0]]) >>> kernel = np.array([[4, 1, 1], [0, 3, 3], [2, 1, 2]]) >>> from scipy.signal import convolve2d >>> convolve2d(arr, kernel[::-1, ::-1]) array([[ 0, 0, 0, 0, 0], [ 2, 3, 7, 4, 4], [ 5, 13, 14, 12, 0], [ 4, 14, 16, 6, 8], [ 1, 4, 7, 12, 0]]) >>> convolve2d(arr, kernel[::-1, ::-1], 'valid') array([[14]]) 处理程序来验证当网格失去焦点时将触发的内容。