I am using a datagrid within another datagrid. My second
datagrid is expand/collapse one. I am facing some error.
This is my code.
ExpandGrid.aspx
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT:
8px; POSITION: absolute; TOP: 8px" runat="server"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3"
OnItemCommand=DataGrid1_ItemCommand GridLines="Horizontal"
Width="503px" AutoGenerateColumns="False"
onItemDataBound="getChildSource">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle
BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C"
BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C"
BackColor="#B5C7DE"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C"
BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:TemplateColumn ItemStyle-Width="9">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/Plus.gif"
CommandName="Expand" ID="btnExpand"
Runat="server"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ItemID" HeaderText="ID" />
<asp:BoundColumn DataField="ItemText" HeaderText="Text" />
<asp:TemplateColumn>
<ItemStyle Width="1" />
<ItemTemplate>
<asp:PlaceHolder ID="ExpandedContent" Visible="False"
Runat="server">
</td></tr>
<tr>
<td width="9"> </td>
<td colspan="3">
<asp:DataGrid id="Datagrid2" runat="server"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3" GridLines="Horizontal"
Width="503px" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle
BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C"
BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C"
BackColor="#B5C7DE"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C"
BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn DataField="Description"
HeaderText="Description" />
</Columns>
</asp:DataGrid>
</asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
Its corresponding c# file is:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace ExpandGrid
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataView dv = new DataView();
dt.Columns.Add("ItemID");
dt.Columns.Add("Item");
dt.Columns.Add("Description");
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "user
id=sa;password=sa;initial catalog=Minutes;data source=
(local)";
myConnection.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select
ItemID,ItemText from Item1",myConnection);
da.Fill(ds,"Item1");
DataGrid1.DataSource = ds;
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);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void DataGrid1_ItemCommand(Object sender,
DataGridCommandEventArgs e)
{
switch( e.CommandName)
{
case "Expand":
PlaceHolder ExpandedContent;
ExpandedContent = (PlaceHolder)e.Item.Cells
[DataGrid1.Columns.Count - 1].FindControl
("ExpandedContent");
ExpandedContent.Visible = (!ExpandedContent.Visible);
ImageButton btnExpand;
btnExpand = (ImageButton)e.Item.Cells[0].FindControl
("btnExpand");
if (btnExpand.ImageUrl == "~/Images/Plus.gif")
{
btnExpand.ImageUrl = "~/Images/Minus.gif";
fetchData();
}
else
btnExpand.ImageUrl = "~/Images/Plus.gif";
break;
}
}
public void fetchData ()
{
SqlConnection myConn = new SqlConnection (
"server=(local);user id=sa;password=sa;
database=Minutes" );
string query = "select ItemID,ItemText from Item1; " +
"select ItemID, Description from Item1";
SqlDataAdapter da = new SqlDataAdapter ( query, myConn );
DataSet ds = new DataSet ( );
da.Fill ( ds );
// define a relationship between the two tables
ds.Relations.Add ( "itemId",
ds.Tables [ 0 ].Columns [ "ItemID" ] ,
ds.Tables [ 1 ].Columns [ "ItemID" ] );
DataGrid1.DataSource = ds.Tables [ 0 ].DefaultView;
DataGrid1.DataBind ();
}
public void getChildSource ( Object src,
DataGridItemEventArgs e )
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataGrid childGrid = ( DataGrid ) e.Item.FindControl
( "Datagrid2" );
childGrid.DataSource = ( ( DataRowView )
e.Item.DataItem ).CreateChildView ( "itemId" );
childGrid.DataBind ( );
}
}
}
}
But when i run this, I am getting the error
The relation is not parented to the table to which this
DataView points.
can anyone kindly help.
thanks