Dear All,

I want to bind a DataSet (with Relations) to Nested Tables also having
buttons for every parent row.. When I click on the Button,I want a new row
to be added to the respective Child Table. The Click event fires, but I
don't see the change in the Child Table.


can anyone help me please!

Thanks.


public class NestedTables : System.Web.UI.Page
     {
          protected System.Web.UI.WebControls.Table Table1;
          DataSet dsMain = new DataSet();
          SqlConnection conn = new SqlConnection(@"User ID=sa;password
=;Initial Catalog=northwind;Data Source=abcd;");

          private void Page_Load(object sender, System.EventArgs e)
          {
               BindData();
          }

          protected void BindData()
          {
               Table1.DataBind();
          }

          #region Web Form Designer generated code
          override protected void OnInit(EventArgs e)
          {
               //
               // CODEGEN: This call is required by the ASP.NET Web Form
Designer.
               //
               InitializeComponent();
               base.OnInit(e);

               SqlDataAdapter adapterMain     = new SqlDataAdapter();
               adapterMain.SelectCommand      = new SqlCommand("select
customerid, companyname, city, country From Customers", conn);
               adapterMain.Fill(dsMain, "Customers");
               adapterMain.SelectCommand      = new SqlCommand("SELECT
OrderID, CustomerID, ShipVia FROM Orders", conn);
               adapterMain.Fill(dsMain, "Orders");

               dsMain.Relations.Add("CustomerOrders",dsMain.Tables
["Customers"].Columns["CustomerID"],dsMain.Tables["Orders"].Columns
["CustomerID"]);
          }

          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
               this.Table1.DataBinding += new System.EventHandler(this
.Table1_DataBinding);
               this.Load += new System.EventHandler(this.Page_Load);

          }
          #endregion

          private void Table1_DataBinding(object sender, System.EventArgs
e)
          {
               TableRow prntRow1;
               TableRow prntRow2;
               TableCell c;
               Table childTable;
               int k=0;
               foreach(DataRow dr in dsMain.Tables["Customers"].Rows)
               {
                    prntRow1=new TableRow();
                    for(int i=0;i<dsMain.Tables
["Customers"].Columns.Count;i++)
                    {
                         c=new TableCell();
                         c.BorderWidth=1;
                         c.Text=Convert.ToString(""+dr[i]);
                         prntRow1.Cells.Add(c);
                    }
                    c=new TableCell();
                    #region Adding a button
                    Button btn=new Button();
                    btn.Text="Add";
                    btn.ID="Add"+k;
                    btn.CommandName="AddMore";
                    btn.CommandArgument=Convert.ToString(k);

                    btn.Command+=new CommandEventHandler(btn_Command);

                    #endregion
                    c.Controls.Add(btn);
                    prntRow1.Cells.Add(c);

                    childTable=new Table();
                    childTable.BorderWidth=1;
                    childTable.ID="childTable"+k;
                    prntRow2=new TableRow();
                    foreach(DataRow drChild in dr.GetChildRows
("CustomerOrders"))
                    {
                         TableRow chldRow=new TableRow();
                         for(int j=0;j<dsMain.Tables
["Orders"].Columns.Count;j++)
                         {
                              TableCell c1=new TableCell();
                              c1.BorderWidth=1;
                              c1.Text=Convert.ToString(""+drChild[j]);
                              chldRow.Cells.Add(c1);
                              childTable.Rows.Add(chldRow);
                         }
                    }
                    c=new TableCell();
                    c.Controls.Add(childTable);

                    c.Attributes["colspan"]=Convert.ToString(""
+dsMain.Tables["Customers"].Columns.Count);
                    prntRow2.Cells.Add(c);

                    Table1.Rows.Add(prntRow1);
                    Table1.Rows.Add(prntRow2);
                    k++;
               }
          }


          private void btn_Command(object sender, CommandEventArgs e)
          {
               Table childTable=(Table)Table1.FindControl("childTable"
+e.CommandArgument);
               TableRow row=new TableRow();
               for(int i=0;i<3;i++)
               {
                    TableCell cell=new TableCell();
                    cell.BorderWidth=1;
                    cell.ForeColor=Color.SkyBlue;
                    row.Cells.Add(cell);
               }

               childTable.Rows.Add(row);
          }
     }
Disclaimer:
The contents of this e-mail are strictly confidential and may be
privileged.  It represents solely the views of the author and is meant only
for the stated recipient/s.
Any unauthorized distribution is strictly prohibited.  F J Benjamin Group
or its subsidiaries will not accept responsibility for any damage caused by
this e-mail or
attachment/s (if any), which have been checked for viruses.

Please visit our website http://www.fjbenjamin.com



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to