Dear All,
The simplest way of create nested datalist with parent child table relation is given below:
protected void loadData()
{
//Note: For Define your connection string i am not writing it because it is in my web.config and i load it through my internal class
DataSet ds = new DataSet();
// my private class that contains sql execute funtions, I pass the SQL parameters and Values through array
UDF myudf = new UDF();
string spName = "bvs_payInvoice";
string[] field = new string[5];
string[] fieldValue = new string[field.Length];
field[0] = "@startDate";
field[1] = "@endDate";
field[2] = "@searchType";
field[3] = "@keyWord";
field[4] = "@status";
fieldValue[0] = startDate;
fieldValue[1] = endDate;
fieldValue[2] = searchType;
fieldValue[3] = keyword;
fieldValue[4] = status;
// this is my own function that execute the data set through passing array parameters
ds = myudf.loadDataSet(spName, field, fieldValue);
// this is an actual trick
// Load two table in one ds (see my next post)
// define Parent column in which you need to create the relation
DataColumn parentColumn1 = ds.Tables[0].Columns[1];
// Define Child column in which you need to create the relation
DataColumn childColumn1 = ds.Tables[1].Columns[1];
// Define relation, the "detailDataset" is the name of your virtual table that you can use in your front end code.. or aspx page
DataRelation dr = new DataRelation("detailDataset", parentColumn1, childColumn1, false);
// Add relation with your ds
ds.Relations.Add(dr);
// Assigning Data source and binding.
DataList1.DataSource = ds;
DataList1.DataBind();
******************
Front End
******************
// Call your main parent dataset fields
<asp:DataList ID="DataList1" >
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "Your column Name")%>
// Create nested datalist
<asp:DataList ID="DataList2" runat="server" datasource='<%# DataBinder.Eval(Container, "DataItem.detailDataset") %>'>
// Call your main Child dataset fields
<ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Your column Name")%></ItemTemplate>
</asp:DataList>
</ItemTemplate>
The simplest way of create nested datalist with parent child table relation is given below:
protected void loadData()
{
//Note: For Define your connection string i am not writing it because it is in my web.config and i load it through my internal class
DataSet ds = new DataSet();
// my private class that contains sql execute funtions, I pass the SQL parameters and Values through array
UDF myudf = new UDF();
string spName = "bvs_payInvoice";
string[] field = new string[5];
string[] fieldValue = new string[field.Length];
field[0] = "@startDate";
field[1] = "@endDate";
field[2] = "@searchType";
field[3] = "@keyWord";
field[4] = "@status";
fieldValue[0] = startDate;
fieldValue[1] = endDate;
fieldValue[2] = searchType;
fieldValue[3] = keyword;
fieldValue[4] = status;
// this is my own function that execute the data set through passing array parameters
ds = myudf.loadDataSet(spName, field, fieldValue);
// this is an actual trick
// Load two table in one ds (see my next post)
// define Parent column in which you need to create the relation
DataColumn parentColumn1 = ds.Tables[0].Columns[1];
// Define Child column in which you need to create the relation
DataColumn childColumn1 = ds.Tables[1].Columns[1];
// Define relation, the "detailDataset" is the name of your virtual table that you can use in your front end code.. or aspx page
DataRelation dr = new DataRelation("detailDataset", parentColumn1, childColumn1, false);
// Add relation with your ds
ds.Relations.Add(dr);
// Assigning Data source and binding.
DataList1.DataSource = ds;
DataList1.DataBind();
******************
Front End
******************
// Call your main parent dataset fields
<asp:DataList ID="DataList1" >
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "Your column Name")%>
// Create nested datalist
<asp:DataList ID="DataList2" runat="server"
<ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Your column Name")%></ItemTemplate>
</asp:DataList>
******************
How you access the nested DataList items
******************
first you need to initialize the controller
DropDownList ddlhrs= default (DropDownList);
HiddenField parentRefId = default (HiddenField);
same like above you need to initialize your all controllers
//PreviewForm is your datalist name
for (int i = 0; i <= previewForm.Items.Count - 1; i++)
{
ddlhrs = (DropDownList)previewForm.Items[i].FindControl("DropDownList1");
// access your control
ddlhrs.SelectedValue.ToString();
//assigned nested datalist to control
dl1 = (DataList)previewForm.Items[i].FindControl("DataList2");
for (int j = 0; j <= dl1.Items.Count - 1; j++)
{
//Assign the Refrence in child datalist controller
parentRefId = (HiddenField)dl1.Items[j].FindControl("parentRefId");
parentRefId.Value.ToString();
}
}
You can use the same technique with Data table, data list, and repeater or data table + repeater or data table + data list or data list + data table in short you create any sort of combination
I hope this post helps you allot
Regards
Rashid Imran Bilgrami
CEO
Best visualization
http://www.bestvisualization.com
Comments
Post a Comment
Thanks for the Comments , Your review will display soon