【问题标题】:Get multiple row DataKeys from gridview从gridview获取多行DataKeys
【发布时间】:2012-03-23 08:20:08
【问题描述】:

目前我有一个 Button Click 事件,它从 Datagrid 中的一行获取 DataKey(通过复选框)并将其分配给 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

【问题讨论】:

    标签: asp.net datagridview foreach datakey


    【解决方案1】:

    重新组织代码。 在第二个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);
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多