Tuesday, 21 May 2013

How to use grid view control and data binding in Asp.net

How to use grid view control and data binding in Asp.net

Step 1 : Create your website first and then create one .aspx page

Step 2 : Open that page and Drag and Drop GridView control from Toolbox Diaglog box

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>


Step 3 : After adding grid view open server side code of that page. opne .cs file or .vb file

Step 4 : Create one class Student and paste below code in file

public class Student
{
    public int RollNo { get; set; }
    public string StudentName { get; set; }
    public string Address { get; set; }
    public string MobileNo { get; set; }
}


Step 5 : After that create 10 student record or paste below code in page load event or in any of the function .

 List<Student> data = new List<Student>();
        data.Add(new Student { RollNo = 1, StudentName = "Student1", MobileNo = "1234567890", Address = "India" });
        data.Add(new Student { RollNo = 2, StudentName = "Student2", MobileNo = "1234567890", Address = "Uk" });
        data.Add(new Student { RollNo = 3, StudentName = "Student3", MobileNo = "1234567890", Address = "India" });
        data.Add(new Student { RollNo = 4, StudentName = "Student4", MobileNo = "1234567890", Address = "Pakistan" });
        data.Add(new Student { RollNo = 5, StudentName = "Student5", MobileNo = "1234567890", Address = "China" });
        data.Add(new Student { RollNo = 6, StudentName = "Student6", MobileNo = "1234567890", Address = "Dubai" });
        data.Add(new Student { RollNo = 7, StudentName = "Student7", MobileNo = "1234567890", Address = "India" });
        data.Add(new Student { RollNo = 8, StudentName = "Student8", MobileNo = "1234567890", Address = "UK" });
        data.Add(new Student { RollNo = 9, StudentName = "Student9", MobileNo = "1234567890", Address = "India" });
        data.Add(new Student { RollNo = 10, StudentName = "Student10", MobileNo = "1234567890", Address = "USA" });



Step 6 : After creating  10 student list data directlly bind that data to Grid View paste below code . and set the grid view control DataSource object and call DataBind method of grid view

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

Step 7 : Just run your application and see see the grid view


No comments:

Post a Comment

Thank you for your interest .