如何从父RowUpdating方法访问嵌套的网格视图行

时间:2014-02-02 23:36:39

标签: c# asp.net gridview

我有一个子网格视图,它添加到父RowDataBound中的父网格视图。在父编辑时,所有子gridview行都可以编辑。这意味着孩子没有自己编辑。如何在父RowUpdating中获取子gridview行以保存已编辑的数据?

 protected void gvEquipment_RowDataBound(object sender, GridViewRowEventArgs e)
  {  
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView gvChild = new GridView();
        gvChild.DataSource = dt;
        gvChild.CssClass = "NestedGrid";
        gvChild.RowDataBound += new GridViewRowEventHandler(gvChild_RowDataBound);

        //Create the show/hide button which will be displayed on each row of the main GridView
        Image imgCollapse = new Image();
        imgCollapse.ID = "btnCollapse";
        imgCollapse.ImageUrl = "~/images/Collapse.gif";
        //Add the javascript function to the show/hide button, passing the row to be toggled as a parameter");
        imgCollapse.Attributes.Add("onclick", "javascript: gvrowtoggle(" + (e.Row.RowIndex + e.Row.RowIndex + 2) + ")");

        //Add the expanded details row after each record in the main GridView
        Table tb1 = e.Row.Parent as Table;
        GridViewRow tr = new GridViewRow(e.Row.RowIndex + 1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
        tr.CssClass = "hidden";
        string sr = (e.Row.RowIndex + e.Row.RowIndex + 2).ToString();
        tr.ID = e.Row.RowIndex.ToString();
        TableCell tc = new TableCell();
        tc.ColumnSpan = gvEquipment.Columns.Count + 3;//e.Row.Cells.Count;
        tc.BorderStyle = BorderStyle.None;
        //tc.BackColor = System.Drawing.Color.WhiteSmoke;
        tc.Controls.Add(gvChild);
        tr.Cells.Add(tc);
        tb1.Rows.Add(tr);
        e.Row.Cells[0].Controls.Add(imgCollapse);
        e.Row.CssClass = "GridAltItem";
        gvChild.DataBind();


     if (IsEditMode && e.Row.RowIndex == EditIndex)
     {
         // edit mode for childGrid
         for (int i = 0; i < dt.Rows.Count; i++)
             for (int j = 0; j < dt.Columns.Count; j++)
             {
                 TextBox txt = new TextBox();
                 txt.Text = dt.Rows[i][j].ToString();
                 gvChild.Rows[i].Cells[j].Controls.Add(txt);
             }
     }
    }
}

 protected void gvEquipment_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
      GridView gv = (GridView)sender;
      //how to get child gridview controls?
  }

3 个答案:

答案 0 :(得分:0)

将“gvChild”控件添加到父GridView后,您可以使用 FindControl 方法获取“gvChild”控件。请参阅下面的示例。

protected void gvEquipment_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = gvEquipment.Rows[e.RowIndex] as GridViewRow;
    GridView gvChild = row.FindControl("gvChild") as GridView;

    //Loop to get data rows
    foreach (GridViewRow item in gvChild.Rows)
    {
        //get data row of gvChild here
    }
}

答案 1 :(得分:0)

我终于找到了孩子gridview:

GridViewRow ParentRow = gvEquipment.Rows[e.RowIndex] as GridViewRow;
Table tb = ParentRow .Parent as Table;
GridView gvChild = (GridView)tb.Rows[e.rowIndex +3].Cells[0].Controls[0];
 foreach (GridViewRow row in gvChild.Rows)
 {
       //do update                         
 }

答案 2 :(得分:0)

但现在的问题是子Grid视图松开了它的编辑值,因为它再次绑定数据绑定。父网格视图具有动态列,并且需要在每个帖子上再次绑定。如何保持子网格值执行更新?