Saturday, May 31, 2014

Code To display data in grid view from Database

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DALBlueWhale.Entities;
using System.Data;

public partial class ManageEmployees : System.Web.UI.Page
{
    Employee _employee;
    DataTable _table;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)

        {
            LoadData();
        }
    }



    private void LoadData()
    {
        _employee = new Employee();
        _table = _employee.GetAllEmployees();
        dgvEmployees.DataSource = _table;
        dgvEmployees.DataBind();
    }

    protected void dgvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgvEmployees.PageIndex = e.NewPageIndex;
    }
    protected void btnAddEmployee_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddEmployee.aspx");
    }
    protected void btnSeach_Click(object sender, EventArgs e)
    {

    }


    protected void dgvEmployees_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "edituser")
        {
            Response.Redirect("AddEmployee.aspx?EmpId="+e.CommandArgument.ToString() +"&state=edit" , true);
        }
        else if (e.CommandName == "viewuser")
        {
            Response.Redirect("AddEmployee.aspx?EmpId=" + e.CommandArgument.ToString() + "&state=view", true);
     
        }
        else if (e.CommandName == "deletuser")
        {
            _employee =new Employee();
            _employee.EmpID = Convert.ToInt32(e.CommandArgument.ToString());
            _employee.Delete();
            this.LoadData();

     
        }
    }
}

0 comments:

Post a Comment