Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. trouble binding datagrid to custom collection

trouble binding datagrid to custom collection

Scheduled Pinned Locked Moved ASP.NET
helpquestiondatabasewpfwcf
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    viktor9990
    wrote on last edited by
    #1

    I'm trying to bind a custom collection to an autogenerated datagrid without success. I'm getting this error: DataGrid with id 'DataGrid2' could not automatically generate any columns from the selected data source. Could you help me to find a solution? What is the problem in my code? Feel free to fix in my code.Thanks. Here is my code (NewsContent.aspx):

    //-------------------------------------------------------------------- Code behind:(NewsContent.aspx.cs). public class NewsContent{ private void Page_Load(object sender, System.EventArgs e) { ListItemCollection myListCollection = AddToCustomCollection(); this.DataGrid2.DataSource = myListCollection; this.DataGrid2.DataBind(); } private ListItemCollection AddToCustomCollection() { ListItemCollection myListCollection = new ListItemCollection(); NewsContent.NewsItem myItem1 = new NewsContent.NewsItem("viko", "url1", "today"); NewsContent.NewsItem myItem2 = new NewsContent.NewsItem("vikan", "url2", "imorgon"); NewsContent.NewsItem myItem3 = new NewsContent.NewsItem("kiki", "url3", "veckan"); myListCollection.Add(myItem1); myListCollection.Add(myItem2); myListCollection.Add(myItem3); return myListCollection; } //Inner Class which holds all properties needed to show news content and listings public class NewsItem { public NewsItem() { } public NewsItem(string linkName, string linkUrl, string date) { } public string linkName; public string linkUrl; public string date; public string longDate; } } //--------------------------------------------------------------------------------- I'm using a collection class: (ListItemCollection.cs): public class ListItemCollection:CollectionBase { public ListItemCollection() { // // TODO: Add constructor logic here // } public NewsContent.NewsItem Item(int index) { //the appropiate item is retrieved from the list object and //explicitly cast to the ListItem type, then returned to the //caller return (NewsContent.NewsItem) List[index]; } public int Add(NewsContent.NewsItem myNewsItem) { return List.Add(myNewsItem); } }

    M 1 Reply Last reply
    0
    • V viktor9990

      I'm trying to bind a custom collection to an autogenerated datagrid without success. I'm getting this error: DataGrid with id 'DataGrid2' could not automatically generate any columns from the selected data source. Could you help me to find a solution? What is the problem in my code? Feel free to fix in my code.Thanks. Here is my code (NewsContent.aspx):

      //-------------------------------------------------------------------- Code behind:(NewsContent.aspx.cs). public class NewsContent{ private void Page_Load(object sender, System.EventArgs e) { ListItemCollection myListCollection = AddToCustomCollection(); this.DataGrid2.DataSource = myListCollection; this.DataGrid2.DataBind(); } private ListItemCollection AddToCustomCollection() { ListItemCollection myListCollection = new ListItemCollection(); NewsContent.NewsItem myItem1 = new NewsContent.NewsItem("viko", "url1", "today"); NewsContent.NewsItem myItem2 = new NewsContent.NewsItem("vikan", "url2", "imorgon"); NewsContent.NewsItem myItem3 = new NewsContent.NewsItem("kiki", "url3", "veckan"); myListCollection.Add(myItem1); myListCollection.Add(myItem2); myListCollection.Add(myItem3); return myListCollection; } //Inner Class which holds all properties needed to show news content and listings public class NewsItem { public NewsItem() { } public NewsItem(string linkName, string linkUrl, string date) { } public string linkName; public string linkUrl; public string date; public string longDate; } } //--------------------------------------------------------------------------------- I'm using a collection class: (ListItemCollection.cs): public class ListItemCollection:CollectionBase { public ListItemCollection() { // // TODO: Add constructor logic here // } public NewsContent.NewsItem Item(int index) { //the appropiate item is retrieved from the list object and //explicitly cast to the ListItem type, then returned to the //caller return (NewsContent.NewsItem) List[index]; } public int Add(NewsContent.NewsItem myNewsItem) { return List.Add(myNewsItem); } }

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, The DataGrid control automatically generates the columns based on the public properties of the dataitem object, so you need to change from a public field in the NewsItem class to a public property.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups