Dear Murugesan22, yes I added header part, too and I made some changes and it has this error message now...
The relation is not parented to the table to which this DataView points.
Would you please help me about it?
<asp:DataList ID="dtlSubjectBox" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"
Width="600" OnItemDataBound="OuterRepItemDataBound">
<ItemTemplate>
<table width="295" border="1" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
<tr>
<td height="20" align="center" valign="middle">
<%# DataBinder.Eval(Container.DataItem, "Title")%>
</td>
</tr>
<tr>
<td align="right" valign="top">
<asp:DataList ID="dtlArticleBox" runat="server">
<ItemTemplate>
<table width="295" cellspacing="0" cellpadding="0">
<tr>
<td height="20" align="right" valign="middle">
<%# ((DataRowView)Container.DataItem).Row.GetChildRows("Relation") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{BindData();
}
}
private void BindData()
{
SqlConnection cnn = new SqlConnection("Data Source=SHERY-PC;Initial Catalog=News;User ID=sa;Password=1234;Connection Timeout=30;");
SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT [Id], [Title], [Confirmed], [OrderIndex] FROM NewsSubject WHERE [Confirmed]=1 ORDER BY [OrderIndex]", cnn);
DataSet ds = new DataSet();
cmd1.Fill(ds, "NewsSubject");
SqlDataAdapter cmd2 = new SqlDataAdapter("SELECT [Id], [Title] FROM NewsArticle WHERE ([Confirmed]=1 AND [SubjectId]=@SubjectId) ORDER BY [Id]", cnn);
cmd2.Fill(ds, "NewsArticle");
ds.Relations.Add("myrelation", ds.Tables["NewsSubject"].Columns["Title"], ds.Tables["NewsArticle"].Columns["Title"]);
dtlSubjectBox.DataSource = ds.Tables["NewsSubject"];
dtlSubjectBox.DataBind();
}
protected void OuterRepItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
DataRowView drv = e.Item.DataItem as DataRowView;
DataList dtlArticleBox = e.Item.FindControl("dtlArticleBox") as DataList;
dtlArticleBox.DataSource = drv.CreateChildView("myrelation");
dtlArticleBox.DataBind();
}
}