Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

Grid within a Grid ItemDataBound event problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Girish - 18 Jul 2005 22:23 GMT
Im trying to create a grid within a grid programmatically. Ive been
successful in doing this but I need the embedded grid to fire its
ItemDataBound event so I can handle it. The event does not seem to fire for
some reason. The code is below.

Look towards the end of the CustomerDataGrid_OnItemDataBound(object sender,
DataGridItemEventArgs e) method. This is where I register the event and
place the programmatically created grid into the main grid. The registered
event does not cause the OrdersDataGrid_ItemDataBound to run.

Appreciate anyones help.

Thanks,
Girish

> ------------
> ASPX File
> -----------
<%@ Page language="c#" Inherits="MasterDetail.CustomerOrderDataGrid"
EnableViewState="False" CodeBehind="CustomerOrderDataGrid.aspx.cs"
AutoEventWireup="false" %>
<HTML>
<body style="FONT: x-small Verdana, Arial, sans-serif">
 <!-- Begin Web Form -->
 <form id="CustomerOrderDataGrid" method="post" runat="server">
  <p><a href="/DayOfDotNet/">Parent Directory</a></p>
  <!-- Begin DataGrid -->
  <asp:DataGrid id="CustomerDataGrid" runat="server"
AutoGenerateColumns="False" CellPadding="2"
   CellSpacing="0" Font-Names="Verdana, Arial, sans-serif"
BorderColor="Black" BorderWidth="1"
   GridLines="Horizontal"
OnItemDataBound="CustomerDataGrid_OnItemDataBound" EnableViewState="False">
   <AlternatingItemStyle BackColor="Tan"></AlternatingItemStyle>
   <ItemStyle Font-Size="X-Small"></ItemStyle>
   <HeaderStyle Font-Size="Small" Font-Names="Arial" Font-Bold="True"
ForeColor="White" BackColor="Maroon"></HeaderStyle>
   <Columns>
    <asp:BoundColumn Visible="False"
DataField="CustomerID"></asp:BoundColumn>
    <asp:HyperLinkColumn
    DataTextField="CustomerID"
    DataNavigateUrlField="CustomerID"
    DataNavigateUrlFormatString="OrderDetailDataGrid.aspx?customerid={0}"
    HeaderText="ID"
    ItemStyle-VerticalAlign="Top" />

    <asp:TemplateColumn HeaderText="Customer">
     <ItemStyle VerticalAlign="Top"></ItemStyle>
     <ItemTemplate>
      <b>
       <%# DataBinder.Eval(Container.DataItem, "CompanyName") %>
      </b>
      <br>
      <%# DataBinder.Eval(Container.DataItem, "Address" ) %>
      <br>
      <%# DataBinder.Eval(Container.DataItem, "City" ) %>
      ,
      <%# DataBinder.Eval(Container.DataItem, "Region") %>
      <%# DataBinder.Eval(Container.DataItem, "PostalCode" ) %>
      <br>
      <br>
      <%# DataBinder.Eval(Container.DataItem, "ContactName" ) %>
      <br>
      <%# DataBinder.Eval(Container.DataItem, "ContactTitle" ) %>
      <br>
      <%# DataBinder.Eval(Container.DataItem, "Phone" ) %>
     </ItemTemplate>
    </asp:TemplateColumn>
          <asp:TemplateColumn ItemStyle-VerticalAlign="Top"
HeaderText="Orders">
      <%-- Embedded DataGrid will go here --%>
     </asp:TemplateColumn>
   </Columns>
  </asp:DataGrid>
  <!-- End DataGrid -->
 </form>
 <!-- End Web Form -->
</body>
</HTML>

---------------------
CODE BEHIND
----------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace MasterDetail
{
public class CustomerOrderDataGrid : System.Web.UI.Page
{
 protected DataGrid CustomerDataGrid;
 private DataSet ds = new DataSet();

 private void Page_Load(object sender, System.EventArgs e)
 {
  string sqlStmt = "SELECT top 2 * FROM Customers; SELECT * FROM Orders";
  string conString =
"server=localhost;database=Northwind;uid=sa;pwd=tietronix;";

  SqlDataAdapter sda = new SqlDataAdapter(sqlStmt, conString);

  sda.Fill(ds);
  ds.Tables[0].TableName = "Customers";
  ds.Tables[1].TableName = "Orders";

  CustomerDataGrid.DataSource = ds.Tables["Customers"];
  CustomerDataGrid.DataBind();
 }

 //Use the OnItemDataBound event handler to dynamically add an embedded
DataGrid
 protected void CustomerDataGrid_OnItemDataBound(object sender,
DataGridItemEventArgs e)
 {
  //When each row is created in the DataGrid, eval the ItemType
  if(e.Item.ItemType == ListItemType.Item ||
   e.Item.ItemType == ListItemType.AlternatingItem)
  {
   //If the ItemType is Item or AlternatingItem,
   //Create a new DataGrid object named OrdersDataGrid
   DataGrid OrdersDataGrid = new DataGrid();

   //Format the DataGrid to look cool.
   OrdersDataGrid.BorderWidth = (Unit)1;
   OrdersDataGrid.CellPadding = 4;
   OrdersDataGrid.CellSpacing = 0;
   OrdersDataGrid.GridLines = GridLines.Horizontal;
   OrdersDataGrid.BorderColor = Color.FromName("Black");

   OrdersDataGrid.ItemStyle.Font.Name = "Verdana";
   OrdersDataGrid.ItemStyle.Font.Size = FontUnit.XSmall;

   OrdersDataGrid.AlternatingItemStyle.BackColor =
Color.FromName("LightGray");

   OrdersDataGrid.ShowHeader = true;
   OrdersDataGrid.HeaderStyle.BackColor = Color.FromName("Black");
   OrdersDataGrid.HeaderStyle.ForeColor = Color.FromName("White");
   OrdersDataGrid.HeaderStyle.Font.Bold = true;
   OrdersDataGrid.HeaderStyle.Font.Size = FontUnit.XSmall;

   //Do not autogenerate columns.
   OrdersDataGrid.AutoGenerateColumns = false;

   //Add a series of BoundColumns
   //Order ID
   BoundColumn bc = new BoundColumn();
   //Set the BoundColumn Values
   bc.HeaderText = "Order ID";
   bc.DataField = "OrderID";
   bc.ItemStyle.Wrap = false;
   //Add the BoundColumn to the OrdersDataGrid.
   OrdersDataGrid.Columns.Add(bc);

   //Order Date
   bc = new BoundColumn();
   bc.HeaderText = "Order Date";
   bc.DataField = "OrderDate";
   bc.DataFormatString="{0:d}";
   bc.ItemStyle.Wrap = false;
   OrdersDataGrid.Columns.Add(bc);

   //Required Date
   bc = new BoundColumn();
   bc.HeaderText = "Required Date";
   bc.DataField = "RequiredDate";
   bc.DataFormatString="{0:d}";
   bc.ItemStyle.Wrap = false;
   OrdersDataGrid.Columns.Add(bc);

   //Shipped Date
   bc = new BoundColumn();
   bc.HeaderText = "Shipped Date";
   bc.DataField = "ShippedDate";
   bc.DataFormatString="{0:d}";
   bc.ItemStyle.Wrap = false;
   OrdersDataGrid.Columns.Add(bc);

   //End BoundColumns
   TemplateColumn tc = new TemplateColumn();
   tc.HeaderText = "Drop Down Menu";
   OrdersDataGrid.Columns.Add(tc);

   //Get the Authors DataView and filter it for the current ISBN
   DataView _orders = ds.Tables["Orders"].DefaultView;
   _orders.RowFilter = "CustomerID='" + e.Item.Cells[0].Text + "'";

   //Bind the DataGrid.
   OrdersDataGrid.DataSource = _orders;
   OrdersDataGrid.DataBind();

   // ADD THE EVENT TO THE EMBEDDED GRID HERE
   OrdersDataGrid.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.OrdersDataGrid_ItemDataBound);
   //Add the OrdersDataGrid to the BooksDataGrid.
   e.Item.Cells[3].Controls.Add(OrdersDataGrid);
  }
 }

 private void OrdersDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
 {
   //CODE NEVER REACHES HERE.
  int i=1;
  i=10 + 12;
 }

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

 private void InitializeComponent()
 {
  this.CustomerDataGrid.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.CustomerDataGrid_ItemCreated);
  //this.CustomerDataGrid.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.CustomerDataGrid_OnItemDataBound);
  this.Load += new System.EventHandler(this.Page_Load);

 }

 private void CustomerDataGrid_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
 {
  int i=1;
  i=10 + 12;
 }
}
}
Phillip Williams - 18 Jul 2005 23:09 GMT
You should add the event handler before you execute the
OrdersDataGrid.DataBind() method.

I have 2 samples of a several-levels hierarchical datagrid working on my
website using both the datagrid server control and the HTML Table:
http://www.societopia.net/samples/webform2.aspx
http://www.societopia.net/samples/webform1.aspx
Signature

http://www.webswapp.com

> Im trying to create a grid within a grid programmatically. Ive been
> successful in doing this but I need the embedded grid to fire its
[quoted text clipped - 238 lines]
>  }
> }
vinay - 18 Jul 2005 23:16 GMT
Nice samples Philip
Signature

http://pathidotnet.blogspot.com
=====
vInAypAtHi
  o__
---_,>/'_------
 (_) \(_)
---------------

> You should add the event handler before you execute the
> OrdersDataGrid.DataBind() method.
[quoted text clipped - 246 lines]
> >  }
> > }
Girish - 19 Jul 2005 00:30 GMT
Thanks Phillip. This helps.

Girish

> You should add the event handler before you execute the
> OrdersDataGrid.DataBind() method.
[quoted text clipped - 252 lines]
>>  }
>> }
Patrick.O.Ige - 19 Jul 2005 02:17 GMT
Good one Phillips!

> You should add the event handler before you execute the
> OrdersDataGrid.DataBind() method.
[quoted text clipped - 47 lines]
> >      DataTextField="CustomerID"
> >      DataNavigateUrlField="CustomerID"

DataNavigateUrlFormatString="OrderDetailDataGrid.aspx?customerid={0}"
> >      HeaderText="ID"
> >      ItemStyle-VerticalAlign="Top" />
[quoted text clipped - 153 lines]
> >     // ADD THE EVENT TO THE EMBEDDED GRID HERE
> >     OrdersDataGrid.ItemDataBound += new

System.Web.UI.WebControls.DataGridItemEventHandler(this.OrdersDataGrid_ItemD
ataBound);
> >     //Add the OrdersDataGrid to the BooksDataGrid.
> >     e.Item.Cells[3].Controls.Add(OrdersDataGrid);
[quoted text clipped - 21 lines]
> >   {
> >    this.CustomerDataGrid.ItemCreated += new

System.Web.UI.WebControls.DataGridItemEventHandler(this.CustomerDataGrid_Ite
mCreated);
> >    //this.CustomerDataGrid.ItemDataBound += new

System.Web.UI.WebControls.DataGridItemEventHandler(this.CustomerDataGrid_OnI
temDataBound);
> >    this.Load += new System.EventHandler(this.Page_Load);
> >
[quoted text clipped - 8 lines]
> >  }
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.