Bind Grid using List Properties

Rajni.Shekhar
Posted by Rajni.Shekhar under ASP.NET category on | Points: 40 | Views : 966
ASPX Page

<asp:GridView ID="gvEmp" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkAll" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkSelection" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" />
</Columns>
</asp:GridView>


in code behind page

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack){
List<MyClass> collectionOfEmp = new List<MyClass>();
collectionOfEmp.Add(new MyClass("Emp1", "Delhi"));
collectionOfEmp .Add(new MyClass("Emp2", "Agra"));
collectionOfEmp .Add(new MyClass("Emp3", "Delhi"));
collectionOfEmp .Add(new MyClass("Emp4", "Delhi"));

this.gvEmp.DataSource = collectionOfEmp;
this.gvEmp.DataBind();
}
}
public class MyClass
{
string _Name;
string _Address;
public MyClass(string Name, string Address)
{
_Name = Name;
_Address1 = Address;
}

public string Name
{
get { return _Name; }
set { _Name = value; }
}

public string Address
{
get { return _Address; }
set { _Address = value; }
}
}

Comments or Responses

Login to post response