简体   繁体   中英

C# foreach statement

I need some help with a for each statement, basically what happens is when a user edits a value within a cell, my foreach will apply this to all cells within the datagrid and change the value of them all, i need my foreach statement to work by iterating through the datagrid but only change the selected row that has been edited

try
{
    //loop through each of the rows in the dgv
    foreach  (DataGridViewRow row in dgvDetials.SelectedRows) 
    {
        int intQtyInsp = 0;

        //if quantity inspected is empty:
        if (row.Cells[2].Value.ToString() == "") 
        {
            //quantity inspected is 0. Prevents null value errors:
            intQtyInsp = 0; 
        }

        intQtyInsp = 
            Int32.Parse(dgvDetials.CurrentRow.Cells[2].Value.ToString());

        if (intQtyInsp < 0)   // checks the cell for a negative value 
        {
            intQtyInsp = 0;   // if cells is negative submits value as Zero
        }
        else
        {
            //sets quantity inspected to value entered
            intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString()); 
        }

        if (intQtyInsp == 0) //if quantity inspected is 0. Ignore row.
        {

        }
        else //else gather details and insert row as production.
        {
            area = dtArea2.Rows[0]["area_code"].ToString();
            inspDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy");
            inspShift = cbShift.Text;
            partNo = row.Cells[0].Value.ToString();
            // dieCode = row.Cells[0].Value.ToString();
            dieCode = "";
            machine = "";
            qtyInsp = intQtyInsp;
            qtyInspRecorded = Int32.Parse(row.Cells[5].Value.ToString());
            comment = "";
            //machine = row.Cells[3].Value.ToString();

            if (qtyInspRecorded == 0)
            {
                SQLMethods.insertProduction(area, 
                                            inspDate,
                                            inspShift, 
                                            partNo, 
                                            dieCode, 
                                            qtyInsp, 
                                            comment, 
                                            machine);
            }
            else
            {
                SQLMethods.updateProduction(area, 
                                            inspDate, 
                                            inspShift, 
                                            partNo, 
                                            dieCode, 
                                            (qtyInspRecorded + qtyInsp), 
                                            comment, 
                                            machine);
            }
        }
    }
    retrieveData(); //reset values
}
catch (Exception ex)
{
    MessageBox.Show(
        "Error instering production values. Processed with error: " 
        + ex.Message);
}    

First of all, I would simplify the code here a little by splitting it into several methods that may be called from the For-loop. That would make it easier to read, and thereby easier to help you too. Just to provide an example, the following:

if (intQtyInsp < 0)   // checks the cell for a negative value 
{
    intQtyInsp = 0;   // if cells is negative submits value as Zero
}
else
{
    //sets quantity inspected to value entered
    intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString()); 
}

could be replaced with something like:

int intQtyInsp = SetQuantityInspected();

Then that method could contain the if-structure. Repeat this for other parts of the code in the loop too. Trust me, this will make your life easier.

Also, it seems as if the result of this section is never used; the value of intQtyInsp is overwritten right afterwards!:

if (row.Cells[2].Value.ToString() == "") 
{
    //quantity inspected is 0. Prevents null value errors:
    intQtyInsp = 0; 
}

As for your question: I'm not sure how you would get the id of the row that is currently being edited. (If possible (?), it might be getter to loop through the table / data source behind the datagrid?).

In any case, what you need to do is something like the following inside your loop:

if(IsCurrentlyEditedRow(row)){
    ...

    // (all the stuff currently in the body of your loop goes here)
    ...
}

Now you can implement the method IsCurrentlyEditedRow() to return True or False depending on whether or not the id of the current row is the the same as that of the one you are editing.

Sorry if this is not a very specific and detailed answer, hope it is of some use anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM