Monday, May 12, 2008

Jagged array in .Net

private void SampleJaggedArray()
{
int[][] jaggedArray;
jaggedArray = new int[2][];

jaggedArray[0] = new int[3];
jaggedArray[1] = new int[6];

jaggedArray[0][0] = 1; jaggedArray[0][1] = 2; jaggedArray[0][2] = 3;

jaggedArray[1][0] = 1; jaggedArray[1][1] = 2; jaggedArray[1][2] = 3;
jaggedArray[1][3] = 4; jaggedArray[1][4] = 5; jaggedArray[1][5] = 6;

for (int i = 0; i < jaggedArray.GetLength(0); i++)
{
Console.Write("jaggedArray[" + i + "] :\t");
for (int j = 0; j < jaggedArray[i].Length; j++)
{
Console.Write(jaggedArray[i][j] + "\t");
}
Console.WriteLine();
}
}

binding DataTable to asp:Repeater

html code :

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0">
<tr><td colspan="2"><b>Personal Database</b></td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="background-color:inherit;" colspan="2"> <%# DataBinder.Eval(Container, "DataItem.Name") %> </td>
</tr>
<tr>
<td> <%# DataBinder.Eval(Container, "DataItem.Age") %>
</td>
<td> <%# DataBinder.Eval(Container, "DataItem.Designation") %>
</td>
</tr>
<tr>
<td>
<a href="<%# DataBinder.Eval(Container, "DataItem.Email") %>">
<%# DataBinder.Eval(Container, "DataItem.Email") %>
</a>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td height="2" colspan="2"><hr /></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>


code-behind :

public partial class DetailsPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("Personal Database");
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(Int32));
dt.Columns.Add("Designation", typeof(string));
dt.Columns.Add("Email", typeof(string));
dt.Rows.Add(new string[4] { "Jack", 24, ".net Programmer", "jackdsouza0@gmail.com" });
dt.Rows.Add(new string[4] { "Jill", 23, ".net Programmer", "jilldsouza0@gmail.com" });
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}