473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

datagrid problem

Reb
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

Nov 15 '05 #1
0 2065

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
0
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some...
5
by: Jeff | last post by:
IDE: VS 2003 :NET OS: XP Pro My app have a form with a tab-control on it. The tab-control have 2 tabpages. One of the tabpages displays a datagrid, and the other tabpage displays details (order...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
4
by: The Alchemist | last post by:
I am having a problem with a dynamically-generated Datagrid. It is important to point out that this problem does not exist with a design-time created Datagrid, but only with a dynamically generated...
9
by: tshad | last post by:
How do I find (and set) a couple of labels in the Footer after a DataGrid is filled? I have a bunch of DataGrids that get displayed nested inside a DataList. The datagrid looks like: ...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.