Grid Operations

Create Grid Manually.....



Source Code


<asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="False"> 

  <Columns>


                            <asp:TemplateField HeaderText="Employee Code">

                                <ItemTemplate>

                                    <%# Eval("Emp_Code")%>

                                </ItemTemplate>

                            </asp:TemplateField>



                            <asp:TemplateField HeaderText="Name">

                                <ItemTemplate>

                                    <%# Eval("Name")%>

                                </ItemTemplate>

                            </asp:TemplateField>



                            <asp:TemplateField HeaderText="Email">

                                <ItemTemplate>

                                    <%#Eval("Email")%>

                                </ItemTemplate>

                            </asp:TemplateField>



                            <asp:TemplateField HeaderText="DOB">

                                <ItemTemplate>

                                    <%# Eval("DOB")%>

                                </ItemTemplate>

                            </asp:TemplateField>



    </Columns>

   </asp:GridView>


NOTE:

<%# Eval("Emp_Code")%>  : Emp_Code  is a variable in the class file "Utility" 
<%# Eval("Name")%> Name  is a variable in the class file "Utility" <%#Eval("Email")%>  Email  is a variable in the class file "Utility"
<%# Eval("DOB")%>   DOB  is a variable in the class file "Utility"
C# Code

  1. Create an asp.net app
  2. Add a webform "AddEmployee.aspx"
  3. Add a class file "Utility.cs"

Utility.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Employee_Managment_System
{
    public class Utility
    { 
        private string name;
        private string emp_Code;
        private string email;
        private DateTime dob;
  
        public string Name
        {
            set { this.name = value; }
            get { return this.name; }
        }
         
        public string Emp_Code
        {
            set { this.emp_Code = value; }
            get { return this.emp_Code; }
        }
        public string Email
        {
            set { this.email = value; }
            get { return this.email; }
        }

        public DateTime DOB
        {
            set { this.dob = value; }
            get { return this.dob; }
        }

 
    SqlConnection conn = new SqlConnection("Data Source=10.18.184.133;Initial       Catalog=Employee_Managment;User ID=niituser;Password=niituser");
        SqlCommand command;
        SqlDataReader reader;
        Utility Emp;

       
        public List<Utility> Get_Employee()
        {
          List<Utility> EmpList = new List<Utility>();

            try
            {
         conn.Open();

         SqlCommand command= new SqlCommand (query,conn);

               SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())

                {

                    Emp = new Utility();

                    Emp.Emp_Code = Convert.ToString(reader["emp_code"]); 

                    Emp.Name = Convert.ToString(reader["name"]);

                    Emp.Email = Convert.ToString(reader["email"]);

                    Emp.DOB = Convert.ToDateTime(reader["Dob"]);

                    EmpList.Add(Emp);         // add each employee row in table to list object        
                 }
            }
            catch (Exception )
            {
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return EmpList;
        }

AddEmployee.aspx

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Employee_Managment_System
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Utility emp = new Utility();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Fill_Grid();
               
            }

        public void Fill_Grid()
        {
            List<Utility> Emp_List = emp.Get_Employee();
            if (Emp_List.Count > 0)
            {
                grdEmployee.DataSource = Emp_List;
                grdEmployee.DataBind();
            }
            else
            {
                empCount = 1;
            }
        }
      }
   }

























No comments:

Post a Comment