SaraniO SaraniO - 8 days ago 9
C# Question

Why is code still running after return

I have some code in

CellValueChanged
event of
datagridview
, the full code is below:

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

Answer

First CellBeginEdit event must be like this:

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

And then CellValueChnaged event must be like this:

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