Home » C++ ProgrammingRSS

Manually populate listview

How can I manually populate a listview control without sql connection?

 

3 Answers Found

 

Answer 1

You can use Generic List Collection of your custom Object Type or Anonymous Type

//Anonymous Type
ListView1.DataSource = Enumerable.Range(1, 10).Select(a => new { ID = a, Name = string.Format("Test Name {0}", a) });
ListView1.DataBind();

//Custom Class
List<MyClass> cls = new List<MyClass>();
cls.Add(new MyClass() { ID = 1, Name = "Test Name 1" });
cls.Add(new MyClass() { ID = 2, Name = "Test Name 2" });
ListView1.DataSource = cls;
ListView1.DataBind();
 

Answer 2

you could use any structure that implements IListSource, IEnumerable or IDataSource, and 

- supply that to the DataSource property
- execute the DataBind() method right after


it would even bind to a string (* markup is 4.0 without a placeholder):

        <asp:ListView ID="ListView1" runat="server">
            <ItemTemplate>
                <%# Container.DataItem %>
            </ItemTemplate>
            <ItemSeparatorTemplate>
                <br />
            </ItemSeparatorTemplate>
        </asp:ListView>

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListView1.DataSource = "data";
                ListView1.DataBind();
            }
        }


 

Answer 3

Bind a Listview with a collection (something like IList<Class> that implements IEnumerable<T>). And use Eval to show the values

there. That's OK. 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter