BINDING (System.Collections.Generic.List) TO GRIDVIEW

THIS EXAMPLES HELPS TOU TO BIND DATAGRID WITH System.Collections.Generic.List ARRAY

PLACE A DATAGRID IN THE DEFAULT.ASPX PAGE AND WRITE THE FOLLOWING CODE



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class classCric : System.Web.UI.Page
{
//this is class
public class cricket
{
private int no;
private string cname;
private string country;
// this is property
public int no1
{
get
{
return no;
}
set
{
no = value;
}
}
public string cname1
{
get
{
return cname;
}
set
{
cname = value;
}
}
public string country1
{
get
{
return country;
}
set
{
country = value;
}
}
public cricket(int no1, string cname1, string country1)
{
this.no = no1;
this.cname = cname1;
this.country = country1;
}

}//end of cricket class


protected void Page_Load(object sender, EventArgs e)
{
System.Collections.Generic.List<cricket> cric = new System.Collections.Generic.List<cricket>();
cric.Add(new cricket(1, "dhoni", "india"));
cric.Add(new cricket(1, "afridi", "pak"));
cric.Add(new cricket(1, "mindas", "sl"));
GridView1.DataSource = cric;
GridView1.DataBind();

}
}


Attachments

  • DATAGRID BINDING (22543-18543-DATAGRID.DOC)
  • Comments

    Author: Jasjit30 Dec 2008 Member Level: Bronze   Points : 0

    This coding is not working

    Author: Navin C Pandit31 May 2011 Member Level: Gold   Points : 1

    Hi Bob,

    Very nice code & thanks to share it, but if you are initializing your class member variables via constructor then there is no use of set{} method in any of property. So, avoid unnecessary code and just define properties as:

    public string country1
    {
    get
    {
    return country;
    }
    }

    And off course this will work


    Have a gr8 day!
    :)



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: