Saturday 22 March 2014

bind data to gridview in asp.net

ASP.NET provides a many for showing data and GridView Control is one of them. GridView control, used  for all CRUD operationdisplay, edit and delete data from different kinds of data sources.
There are many way to bind data like data source, XML, Business object(data reader) , business object that exposes data.
There are two main properties which used to bind the data to grid;
DataSource – it is property
Databind – it is method
Gridview create  it's rows  and columns automatically with default options and then displays it immediately.
 
@Code  Snippet for HTML – for simple grid control only;
<body>
    <form id="form1" runat="server">
    <div>
       
        <asp:GridView ID="grdCiew" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
@Creating object value
public List<Employee> BindData()
        {
            List<Employee> objCEmployee = new List<Employee>();
            objCEmployee.Add(new Employee { Id = 1, Name = "Bob", Address = "c/12, delhi", PhoneNumber = "1234567890", Pincode = "123456" });
            objCEmployee.Add(new Employee { Id = 2, Name = "Marina", Address = "D-40, Bombay", PhoneNumber = "5643214589", Pincode = "666 666" });
            objCEmployee.Add(new Employee { Id = 3, Name = "Sumit", Address = "123, Street 2, Hyderbad", PhoneNumber = "876743213", Pincode = "765644" });
            return objCEmployee;
        }
@Bind the Values into grid
protected void Page_Load(object sender, EventArgs e)
        {
            grdCiew.DataSource = BindData().ToList();
            grdCiew.DataBind();
          
        }
@OutPut

No comments:

Post a Comment