How to Add a Datatable column names as a headers in repeater control

Posted by Sandeepkumarvutukuri under ASP.NET on 3/10/2015 | Points: 10 | Views : 5086 | Status : [Member] | Replies : 2
How to Add dynamically a Datatable column names as repeater headers in repeater control.




Responses

Posted by: Bandi on: 3/11/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer
http://stackoverflow.com/questions/2748108/repeater-and-add-columns-to-header-and-itemtemplate-on-page-load
http://forums.asp.net/t/1188170.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sandeepkumarvutukuri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Subhacomm on: 3/12/2015 [Member] Starter | Points: 25

Up
0
Down
Just as StackOverflow you can use the following code also...

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="rowRepeater" OnItemDataBound="rowRepeater_ItemBound">
<HeaderTemplate>
<table border="1px SOLID #666">
<tr>
<asp:Repeater runat="server" ID="headerRepeater">
<ItemTemplate>
<th>
<%# Eval("LocationNm")%>
</th>
</ItemTemplate>
</asp:Repeater>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<asp:Repeater runat="server" ID="columnRepeater">
<ItemTemplate>
<td>
<asp:TextBox ID="txtLocation" runat="server" Text=''></asp:TextBox>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>


and the code behind is

public partial class repeatertest : System.Web.UI.Page
{

SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=EDMtest;Persist Security Info=True;User ID=sa;Password=truser");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter DA;
DataSet LDA;

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
LDA = new DataSet();
conn.Open();
DA = new SqlDataAdapter("SELECT * FROM Location", conn);
DA.Fill(LDA);
rowRepeater.DataSource = LDA;
rowRepeater.DataBind();
conn.Close();
}
}

protected void rowRepeater_ItemBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Repeater headerRepeater = e.Item.FindControl("headerRepeater") as Repeater;
headerRepeater.DataSource = LDA;
headerRepeater.DataBind();
}
else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater columnRepeater = e.Item.FindControl("columnRepeater") as Repeater;
columnRepeater.DataSource = LDA;
columnRepeater.DataBind();
}
}

}


Sandeepkumarvutukuri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response