Gridview - 检查嵌套的gridview行

时间:2014-07-30 15:44:43

标签: c# gridview

我有两个网格视图:父母和孩子。我的目标是使用子表中的值更新父表(这将在数据库中完成)。为了完成这个,我需要从子gridview中读取值。我创建了下面的代码但是没有正确读取子行(我的gettin索引超出了范围错误):

    protected void btnUpdateSelected_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow row_master in gvParent.Rows)
            {
                if (row_master.RowType == DataControlRowType.DataRow)
                {
                    GridView gvChild = (GridView)gvParent.Rows[row_master.RowIndex].FindControl("gvChild");

                    if (gvChild != null)
                    {
                    foreach (GridViewRow row_child in gvChild.Rows)
                    {
                        if (row_child.RowType == DataControlRowType.DataRow)
                        {
                            CheckBox chckboxChild = (CheckBox)row_child.FindControl("chckboxChild");

                            if (chckboxChild.Checked == true & gvChild.Rows.Count == 1)
                            {
                                int index_master = Convert.ToInt32(row_master.RowIndex.ToString());
                                int index_child = Convert.ToInt32(row_child.RowIndex.ToString());

                                int rowID_a = Convert.ToInt32(gvParent.DataKeys[index_master].Values["ID"]);
                                int rowID_b = Convert.ToInt32(gvChild.DataKeys[index_master].Values["ID"]);
                                int serverID = Convert.ToInt32(gvParent.DataKeys[index_master].Values["ServerID"]);
                                int id = Convert.ToInt32(gvChild.DataKeys[index_child].Values["DeviceID"]);
                                int activeStatusID = Convert.ToInt32(gvChild.DataKeys[index_child].Values["ActiveStatusID"]);
                                string managedBy = gvChild.DataKeys[index_child].Values["ManagedBy"].ToString();
                                string monitoredBy = gvChild.DataKeys[index_child].Values["MonitoredBy"].ToString();
                                int managementProtocolID = Convert.ToInt32(gvChild.DataKeys[index_child].Values["ManagementProtocolID"]);

                            }
                        }
                    }
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

0 个答案:

没有答案