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="#E 7E7FF" BorderStyle="No ne" BorderWidth="1p x"
BackColor="Whit e" CellPadding="3"
OnItemCommand=D ataGrid1_ItemCo mmand GridLines="Hori zontal"
Width="503px" AutoGenerateCol umns="False"
onItemDataBound ="getChildSourc e">
<SelectedItemSt yle Font-Bold="True" ForeColor="#F7F 7F7"
BackColor="#738 A9C"></SelectedItemSty le>
<AlternatingIte mStyle
BackColor="#F7F 7F7"></AlternatingItem Style>
<ItemStyle ForeColor="#4A3 C8C"
BackColor="#E7E 7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F 7F7"
BackColor="#4A3 C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3 C8C"
BackColor="#B5C 7DE"></FooterStyle>
<PagerStyle HorizontalAlign ="Right" ForeColor="#4A3 C8C"
BackColor="#E7E 7FF" Mode="NumericPa ges"></PagerStyle>
<Columns>
<asp:TemplateCo lumn ItemStyle-Width="9">
<ItemTemplate >
<asp:ImageButto n ImageUrl="~/Images/Plus.gif"
CommandName="Ex pand" ID="btnExpand"
Runat="server"> </asp:ImageButton >
</ItemTemplate>
</asp:TemplateCol umn>
<asp:BoundColum n DataField="Item ID" HeaderText="ID" />
<asp:BoundColum n DataField="Item Text" HeaderText="Tex t" />
<asp:TemplateCo lumn>
<ItemStyle Width="1" />
<ItemTemplate >
<asp:PlaceHolde r ID="ExpandedCon tent" Visible="False"
Runat="server">
</td></tr>
<tr>
<td width="9"> </td>
<td colspan="3">
<asp:DataGrid id="Datagrid2" runat="server"
BorderColor="#E 7E7FF" BorderStyle="No ne" BorderWidth="1p x"
BackColor="Whit e" CellPadding="3" GridLines="Hori zontal"
Width="503px" AutoGenerateCol umns="False">
<SelectedItemSt yle Font-Bold="True" ForeColor="#F7F 7F7"
BackColor="#738 A9C"></SelectedItemSty le>
<AlternatingIte mStyle
BackColor="#F7F 7F7"></AlternatingItem Style>
<ItemStyle ForeColor="#4A3 C8C"
BackColor="#E7E 7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F 7F7"
BackColor="#4A3 C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3 C8C"
BackColor="#B5C 7DE"></FooterStyle>
<PagerStyle HorizontalAlign ="Right" ForeColor="#4A3 C8C"
BackColor="#E7E 7FF" Mode="NumericPa ges"></PagerStyle>
<Columns>
<asp:BoundColum n DataField="Desc ription"
HeaderText="Des cription" />
</Columns>
</asp:DataGrid>
</asp:PlaceHolder >
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
</form>
Its corresponding c# file is:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
namespace ExpandGrid
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data Grid DataGrid1;
private void Page_Load(objec t sender, System.EventArg s e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataView dv = new DataView();
dt.Columns.Add( "ItemID");
dt.Columns.Add( "Item");
dt.Columns.Add( "Descriptio n");
SqlConnection myConnection = new SqlConnection() ;
myConnection.Co nnectionString = "user
id=sa;password= sa;initial catalog=Minutes ;data source=
(local)";
myConnection.Op en();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter( "Select
ItemID,ItemText from Item1",myConnec tion);
da.Fill(ds,"Ite m1");
DataGrid1.DataS ource = ds;
DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
public void DataGrid1_ItemC ommand(Object sender,
DataGridCommand EventArgs e)
{
switch( e.CommandName)
{
case "Expand":
PlaceHolder ExpandedContent ;
ExpandedContent = (PlaceHolder)e. Item.Cells
[DataGrid1.Colum ns.Count - 1].FindControl
("ExpandedConte nt");
ExpandedContent .Visible = (!ExpandedConte nt.Visible);
ImageButton btnExpand;
btnExpand = (ImageButton)e. Item.Cells[0].FindControl
("btnExpand" );
if (btnExpand.Imag eUrl == "~/Images/Plus.gif")
{
btnExpand.Image Url = "~/Images/Minus.gif";
fetchData();
}
else
btnExpand.Image Url = "~/Images/Plus.gif";
break;
}
}
public void fetchData ()
{
SqlConnection myConn = new SqlConnection (
"server=(local) ;user id=sa;password= sa;
database=Minute s" );
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.Ad d ( "itemId",
ds.Tables [ 0 ].Columns [ "ItemID" ] ,
ds.Tables [ 1 ].Columns [ "ItemID" ] );
DataGrid1.DataS ource = ds.Tables [ 0 ].DefaultView;
DataGrid1.DataB ind ();
}
public void getChildSource ( Object src,
DataGridItemEve ntArgs e )
{
if (e.Item.ItemTyp e == ListItemType.It em ||
e.Item.ItemType == ListItemType.Al ternatingItem)
{
DataGrid childGrid = ( DataGrid ) e.Item.FindCont rol
( "Datagrid2" );
childGrid.DataS ource = ( ( DataRowView )
e.Item.DataItem ).CreateChildVi ew ( "itemId" );
childGrid.DataB ind ( );
}
}
}
}
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