Editable GridView & Row Level Binding

Original Post: http://morewally.com/cs/blogs/wallym/archive/2006/07/20/190.aspx

I've finally started playing with the GridView in ASP.NET 2.0 beyond just using it to display some data.  I wanted to create something similar to a fully editable datagrid that I used in ASP.NET 1.1.  I have created the following GridView below.  With the GridView, one uses the <asp:TemplateField> tag as oppossed to the <asp:TemplateColumn> in ASP.NET 1.1 with a GridView.  This was kinda confusing, but easily resolved. 

The next thing to do was to bind a grid within one of the cells.  This was a mulit-step process.  The first step was to create the GridView within the cell.  That was easy enough.  The next step was to hook in the appropiate event  This event was the OnRowDataBound.  The last step is to get a reference to the GridView within the row.  This can be done by the command: (GridView)e.Row.FindControl("gvChildData");.  And with that, my problems were solved.

The final question is "Is there a better way to do this?"  If so, please let me know.  I'm always trying to learn something new.

        <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvTest_OnRowDataBound">
            <Columns>
                <asp:TemplateField HeaderText = "Next Step">
                    <ItemTemplate>
                        <asp:TextBox Runat="server" ID="NextStep" Text='<%#Eval("NextStep")%>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText = "Child Data">
                    <ItemTemplate>
                        <asp:GridView ID="gvChildData" runat="server"  AutoGenerateColumns="False">
                            <Columns>
                                <asp:BoundField HeaderText="Child Data" DataField="ChildData" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


     protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataRow drData;
            DataTable dtData = new DataTable();
            dtData.Columns.Add(new DataColumn("NextStep"));
            drData = dtData.NewRow();
            drData["NextStep"] = "Step 1";
            dtData.Rows.Add(drData);
            drData = dtData.NewRow();
            drData["NextStep"] = "Step 2";
            dtData.Rows.Add(drData);
            this.gvTest.DataSource = dtData;
            this.gvTest.DataBind();
        }
    }

    protected void gvTest_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        DataRow drData;
        DataTable dtData = new DataTable();
        GridView gvChildData;
        dtData.Columns.Add(new DataColumn("ChildData"));
        drData = dtData.NewRow();
        drData["ChildData"] = System.DateTime.Now.ToString();
        dtData.Rows.Add(drData);
        drData = dtData.NewRow();
        drData["ChildData"] = System.DateTime.Now.ToString();
        dtData.Rows.Add(drData);
        gvChildData = (GridView)e.Row.FindControl("gvChildData");
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (null != gvChildData)
            {
                gvChildData.DataSource = dtData;
                gvChildData.DataBind();
            }
        }
    }


No Comments