Can anybody guide me to generate columns dynamically in listview like we can do for GridView in ASP.net?
No such property exist for ListView control. Instead of hard coding the columns in the page you can generate this from code behind like below.
class Movie
{
public string Title {get;set;}
}
ListView1.DataSource = new List<Movie>()
{
new Movie {Title = "tEST"},
new Movie {Title = "tEST"},
new Movie {Title = "tEST"},
new Movie {Title = "tEST"}
};
ListView1.DataBind();
var columns = new string[]
{
"Coulmn 1",
"Coulmn 2",
"Coulmn 3"
};
var row = ListView1.FindControl("header") as HtmlTableRow;
if (row != null)
{
foreach (var column in columns)
{
HtmlTableCell cell = new HtmlTableCell();
cell.InnerText = column;
row.Cells.Add(cell);
}
}
<asp:ListView runat="server" ID="ListView1" >
<LayoutTemplate>
<table runat="server" id="table1" >
<tr id="header" runat="server">
</tr>
<tr runat="server" id="itemPlaceholder" ></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("Title") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Thanks
Deepu