为什么代码在返回后还在运行?



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

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");
    }
}

第一个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");
    }

实际上你的代码是不完整的,但应该有一个明显的原因。面对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立即被触发。因此,只要当前执行的程序行到达前面提到的change -cell-value行,事件就会再次执行。另一种方法是使用Leave Handler来验证当网格失去焦点时哪个将被触发。

相关内容

  • 没有找到相关文章

最新更新