HTML
GridView ID="grdStudentList" runat="server" AutoGenerateColumns="False"
Width="50%" onrowdatabound="grdStudentList_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="StudentName" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Student Age " ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Address" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
CODE BEHIND
public partial class TestDesign_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadAllStudent();
}
}
private void LoadAllStudent()
{
List<Student> list = new List<Student>();
list.Add(new Student("Samim", 39,"Dhaka"));
list.Add(new Student("Asad", 48,"Chittagong"));
list.Add(new Student("Nayed", 39,"Sylhet"));
list.Add(new Student("Anwar", 48,"Dhaka"));
grdStudentList.DataSource = list;
grdStudentList.DataBind();
}
//I have created Student class
public class Student
{
public Student(string StudentName, int StudentAge, string Address)
{
_StudentName = StudentName;
_StudentAge = StudentAge;
_Address = Address;
}
private string _Address;
public string Address
{
get { return _Address; }
set { _Address = value; }
}
private int _StudentAge;
public int StudentAge
{
get { return _StudentAge; }
set { _StudentAge = value; }
}
private string _StudentName;
public string StudentName
{
get { return _StudentName; }
set { _StudentName = value; }
}
}
protected void grdStudentList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Student entry = e.Row.DataItem as Student;
e.Row.Cells[0].Text = entry.StudentName;
e.Row.Cells[1].Text = entry.StudentAge.ToString();
e.Row.Cells[2].Text = entry.Address;
}
}
}