2

Currently I have a Button Click event which takes the DataKey from a row in a Datagrid (via a checkbox) and assigns it to an INT variable to Update an Individual in a databsse. Everything works fine when I only check 1 checkbox and run the Update. However I need the User to have the option to select all checkboxes and take all those Datakeys and assign it to the same variable

INT oNewParentID

Like I said evrything works fine when only 1 checkbox is Selected. I guess I'm asking how to get an Uknoiwn amount of Datakeys or all DataKeys selected by all Checkboxes and store them in the Variable above to run the Update for all Individuals Selected. Here is my Button Click Event so far which is working for 1 checkbox selected:

    protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
    foreach (GridViewRow row in gvSalesmanCustomers.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("chkSalesCustSelector");
        if (cb != null && cb.Checked)
        {


            int oIndividualID = Convert.ToInt32((gvSalesmanCustomers.DataKeys[row.RowIndex].Value));



            foreach (GridViewRow r in gvSalesmanByManager.Rows)
            {
                CheckBox chkBox = (CheckBox)r.FindControl("chkManagerSalesSelector");
                if (chkBox != null && chkBox.Checked)
                {

                    int oNewParentID = Convert.ToInt32((gvSalesmanByManager.DataKeys[r.RowIndex].Value));



                    Individual ind = new Individual();
                    ind.ReassignIndividual(oIndividualID, oNewParentID);

                    chkBox.Checked = false;
                    cb.Checked = false;

                }


            }



        }


    }

}

And her is my Update Stored Procedure :

CREATE DEFINER=`root`@`%` PROCEDURE `reassign_Individual`(
IN oIndividualID     int(11),
IN oNewParentID         int(11)
)
BEGIN
Update intelliair.individual
set ParentID = oNewParentID
Where IndividualID = oIndividualID;END

My DataGridViews UI

EB.
  • 2,627
  • 8
  • 35
  • 55

1 Answers1

0

Re-organized code. Added break to second foreach assuming that there's no reason to ReasignInvidual multiple times just to assign it to the last selected manager.

protected void imgbtnReassgin_Click(object sender, ImageClickEventArgs e)
{
    List<int> ids = new List<int>();
    foreach (GridViewRow row in gvSalesmanCustomers.Rows)
    {
       CheckBox cb = (CheckBox)row.FindControl("chkSalesCustSelector");
       if (cb != null && cb.Checked)
       {
            int oIndividualID = Convert.ToInt32((gvSalesmanCustomers.DataKeys[row.RowIndex].Value));
            ids.Add(oIndividualID);

            cb.Checked = false;

        }
    }

    int oNewParentID = 0;
    foreach (GridViewRow r in gvSalesmanByManager.Rows)
    {
        CheckBox chkBox = (CheckBox)r.FindControl("chkManagerSalesSelector");
        if (chkBox != null && chkBox.Checked)
        {
             oNewParentID = Convert.ToInt32((gvSalesmanByManager.DataKeys[r.RowIndex].Value));
             chkBox.Checked = false;
             break; //no reason to go with the same logic to next records
             //if you want to uncheck all the checkboxes continue with specific code just for that purpose
        }
    } 


   if (ids.Count > 0 && oNewParentID > 0)
        foreach(var id in ids)
        {
            Individual ind = new Individual();
            ind.ReassignIndividual(id, oNewParentID);
        }

}
B0Andrew
  • 1,725
  • 13
  • 19