Editable GridView & Row Level Binding - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

News

Please goy buy 3-4 copies of my book on MonoTouch titled "Professional iPhone Programming with MonoTouch for .NET/C# Developers." They make great gifts all year round. Plus, I get about $.20 when you buy a copy.

Technical Sites

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, MonoTouch, MonoDroid, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, Atlas, Microsoft AJAX Library, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

Editable GridView & Row Level Binding

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();
            }
        }
    }



Comments

 

ASP.NET Podcast said:



Subscribe
– Everybody is doing it



Download
– The wimpy way.



Show Notes:

Star ...
July 28, 2006 10:56 AM
 

Weblog said:

October 13, 2006 4:20 PM
 

ASP.NET Podcast said:

Subscribe – Everybody is doing it This show is available in the subscription feed. Show Notes: Star Trek/2.0
December 20, 2006 8:27 PM
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems