1

我在另一个中继器内的中继器内有一个 Gridview。声明性代码如下所示:

<asp:Repeater id="parentRepeater" runat="server">
      <itemtemplate>
            <b> <%# DataBinder.Eval(Container.DataItem, "Name") %></b>
            <div>
               <asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' OnItemDataBound="getNestedData">
                    <itemtemplate>
                         <div><%# DataBinder.Eval(Container.DataItem, "Owner") %></div>
                         <div><asp:GridView ID="Grd" runat="server" AutoGenerateColumns="false" OnRowEditing="EditRecord" HorizontalAlign="Left" Width="100%">
                        </asp:GridView></div>
                </itemtemplate>
                </asp:repeater>
            </div>  
       </itemtemplate>
</asp:repeater>

命令式代码隐藏如下所示:

public void Page_Load(object sender, EventArgs e)
{
        SqlConnection cnn = new SqlConnection("asdf");
        SqlDataAdapter cmd1 = new SqlDataAdapter("select * from tblNames", cnn);
        DataSet ds = new DataSet();
        cmd1.Fill(ds, "names");
        SqlDataAdapter cmd2 = new SqlDataAdapter("select * from tblThings", cnn);
        cmd2.Fill(ds, "things");
        ds.Relations.Add("myrelation",
        ds.Tables["names"].Columns["id"],
        ds.Tables["things"].Columns[NameID"]);
        parentRepeater.DataSource = ds.Tables["names"];
        Page.DataBind();
        cnn.Close();
}

这样将设置父中继器和子中继器,当子中继器具有 itemdatabound 时,会发生以下绑定 gridview 的情况:

 public void getNestedData(Object Sender, RepeaterItemEventArgs e)
    {
        GridView subGridView = (GridView)e.Item.FindControl("Grd");
        DataRow rowView = (DataRow)e.Item.DataItem;
        Int32 key = (Int32)rowView["id"];

        SqlConnection cnn = new SqlConnection("asdf");
        SqlDataAdapter cmd1 = new SqlDataAdapter("select * from tblSubThings WHERE ThingID = " + key, cnn);
        DataSet ds = new DataSet();
        cmd1.Fill(ds, "subThings");
        subGridView.DataSource = ds.Tables["subThings"];

        TemplateField edit = new TemplateField();
        edit.ItemTemplate = new editGridViewTemplate(DataControlRowType.DataRow, "edit");
        subGridView.Columns.Add(edit);

        TemplateField Notes = new TemplateField();
        Notes.ItemTemplate = new GridViewTemplate3(DataControlRowType.DataRow, "Notes");
        Notes.HeaderTemplate = new GridViewTemplate3(DataControlRowType.Header, "Notes");
        Notes.EditItemTemplate = new NotesEditGridViewTemplate(DataControlRowType.DataRow, "Notes");
        subGridView.Columns.Add(Notes);

        subGridView.DataBind();
}

这段代码一切正常。它显示了两个中继器内的gridview,带有一个编辑按钮。当编辑按钮被触发时,gridview 应该进入编辑模式(并使用 EditItemTemplate)。但是,调用 EditRecord 时,gridview 不会进入编辑模式。这是 EditRecord 的样子:

public void EditRecord(object sender, GridViewEditEventArgs e)
{
        GridView subGridView = (GridView)sender;
        subGridView.EditIndex = e.NewEditIndex;
        subGridView.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;
        DataBind();
}

有谁知道如何让我的 GridView 进入编辑模式?

4

0 回答 0