473,320 Members | 1,766 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,320 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 2058

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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.