Repeater for MasterDetail


————————
.ASPX
————————

<div>
<asp:DataList ID=”DataList1″ runat=”server”
onitemdatabound=”DataList1_ItemDataBound”>
<ItemTemplate>
<asp:Label ID=”lblCityName” runat=”server”
Text='<%# Eval(“CityName”) %>’>
</asp:Label>

<asp:Repeater ID=”rpt” runat=”server”>
<ItemTemplate>
<br />
<asp:Label
ID=”lblprj” runat=”server”
Text='<%# Eval(“ProjectName”) %>’>
</asp:Label>
</ItemTemplate>
</asp:Repeater>

</ItemTemplate>
</asp:DataList>
</div>

————————
.CS
————————

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

DataTable dt = new DataTable();
DataColumn dc = new DataColumn(“CityName”);
DataColumn dc1 = new DataColumn(“ProjectName”);

dt.Columns.Add(dc);
dt.Columns.Add(dc1);

DataRow dr = dt.NewRow();
dr[0] = “Ahmedabad”;
dr[1] = “Project 1”;
dt.Rows.Add(dr);

DataRow dr0 = dt.NewRow();
dr0[0] = “Ahmedabad”;
dr0[1] = “Project 2”;
dt.Rows.Add(dr0);

DataRow dr1 = dt.NewRow();
dr1[0] = “Baroda”;
dr1[1] = “Project 1”;
dt.Rows.Add(dr1);

mydata = dt;
DataTable dtCity = SelectDistinct(mydata, “CityName”);

DataList1.DataSource = dtCity;
DataList1.DataBind();
}
}

public DataTable mydata
{
get
{
if (ViewState[“mydata”] == null)
ViewState[“mydata”] = new DataTable();

return (DataTable)ViewState[“mydata”];
}
set
{
ViewState[“mydata”] = value;
}
}

private bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
{
bool areEqual = true;

for (int i = 0; i < fieldNames.Length; i++)
{
if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
{
areEqual = false;
break;
}
}

return areEqual;
}

private DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
{
foreach (string field in fieldNames)
newRow[field] = sourceRow[field];

return newRow;
}

private void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
{
for (int i = 0; i < fieldNames.Length; i++)
lastValues[i] = sourceRow[fieldNames[i]];
}

private DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
{
object[] lastValues;
DataTable newTable;
DataRow[] orderedRows;

if (FieldNames == null || FieldNames.Length == 0)
throw new ArgumentNullException(“FieldNames”);

lastValues = new object[FieldNames.Length];
newTable = new DataTable();

foreach (string fieldName in FieldNames)
newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

orderedRows = SourceTable.Select(“”, string.Join(“, “, FieldNames));

foreach (DataRow row in orderedRows)
{
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
{
newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

setLastValues(lastValues, row, FieldNames);
}
}
return newTable;
}

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{

DataTable dtProject = mydata.Clone();
DataRow[] dr = mydata.Select(“CityName='” + ((Label)e.Item.FindControl(“lblCityName”)).Text + “‘”);

for (int i = 0; i < dr.Length; i++)
{
DataRow objdr = dtProject.NewRow();
objdr[0] = dr[i][0];
objdr[1] = dr[i][1];
dtProject.Rows.Add(objdr);
}

((Repeater)e.Item.FindControl(“rpt”)).DataSource = dtProject;
((Repeater)e.Item.FindControl(“rpt”)).DataBind();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s