Listview with autogenerate columns

Go To StackoverFlow.com

0

Can anybody guide me to generate columns dynamically in listview like we can do for GridView in ASP.net?

2012-04-04 08:28
by Ashwani K
There is no such property supported by ListView. Why don't you want to use GridView - Pankaj 2012-04-04 08:48
@Helper GridView doesn't support inserting data just displaying and editing, not 100% sure thats the reason but it would be a valid one - Peter 2015-11-20 15:25


0

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

2012-04-04 09:07
by Deepu Madhusoodanan
Ads