2

目前我有一个按钮单击事件,它从数据网格中的一行中获取数据键(通过复选框)并将其分配给一个 INT 变量以更新数据库中的个人。当我只选中 1 个复选框并运行更新时,一切正常。但是我需要用户可以选择所有复选框并获取所有这些数据键并将其分配给同一个变量

INT oNewParentID

就像我说的,当只有 1 个复选框被选中时,一切都可以正常工作。我想我在问如何获取未知数量的 Datakeys 或所有 Checkboxes 选择的所有 DataKeys 并将它们存储在上面的变量中,以便为所有选定的个人运行更新。到目前为止,这是我的按钮单击事件,它适用于选中的 1 个复选框:

    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;

                }


            }



        }


    }

}

她是我的更新存储过程:

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

我的 DataGridViews 用户界面

4

1 回答 1

0

重新组织代码。foreach假设没有理由ReasignInvidual多次将其分配给最后选择的经理,则将中断添加到第二个。

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

}
于 2014-07-17T11:15:29.897 回答