<asp:datagrid id="DataGrid1" ShowHeader =false AutoGenerateColumns =true runat="server" DataKeyField="OrderId" OnItemDataBound="ItemDB">
<Columns>
<asp:TemplateColumn >
<ItemTemplate>
<b><%#DataBinder.Eval(Container.DataItem,"OrderId")%></b>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
SqlConnection cn;
SqlDataAdapter da;
DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
cn= new SqlConnection ("Server=localhost;uid=sa;pwd=;database=northwind");
if (!Page.IsPostBack)
{
da= new SqlDataAdapter ("SELECT orderid FROM orders ", cn);
ds= new DataSet ();
da.Fill (ds, "Orders");
DataGrid1.DataSource = ds;
DataGrid1.DataBind ();
}
}
protected void ItemDB(Object sender,DataGridItemEventArgs e )
{
if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem ))
{
DataGrid dgDetails = new DataGrid();
int orderid =(int) ((DataRowView)e.Item.DataItem)["OrderID"] ;
dgDetails.DataSource = GetOrderDetails(orderid );
dgDetails.DataBind();
e.Item.Cells[1].Controls.Add(dgDetails);
}
}
DataSet GetOrderDetails(int id )
{
da= new SqlDataAdapter ("SELECT * FROM [Order Details] where orderid= " + id, cn);
ds= new DataSet ();
da.Fill (ds, "OrderDetails");
return ds;
}