Saturday, November 22, 2014

Binding List to GridView

I encounter this error when I try to write a simple gridView. If you face the same issue, hope this help you. I found the solution here . The one that I underlined is where I missed out. :)

The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
public class Item
{
        public int Id {get; set;}

        public string Name { get; set; }
}
public partial class WebForm1 : System.Web.UI.Page
{
       protected void Page_Load(object sender, EventArgs e)

   {
        List<Item> ar = new List<Item>{};

        Item item = new Item(){Id = 1 , Name = "User1"};
        ar.Add(item);
        item.Id = 2;
        item.Name= "User2";

        ar.Add(item);

        GridView1.DataSource = ar;
        GridView1.DataBind(); 

   }
}


}

wondering why you now see 2 User2 in the gridView? Then, you have to take a look on how to fill in the list .

No comments:

Post a Comment