Wednesday, July 21, 2010

ListView control example

I am giving the main codes below.If u need
the project with all coding then contact with me.

This program will add,delete user input to a listview box,
it also shows the currently selected employee name
in listview box.

using System;
using System.Collections.Generic;
using System.Text;

namespace MridulSamadder
{
    public enum DesignationType
    {
        SSE,
        SE,
        TeamLead
    }
   public class Employee
    {
        private string id;

        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string designation;

        public string Designation
        {
            get { return designation; }
            set { designation = value; }
        }
        private DateTime dateOfBirth;

        public DateTime DateOfBirth
        {
            get { return dateOfBirth; }
            set { dateOfBirth = value; }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MridulSamadder
{
    static class Entry
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new EmployeeInformationUI());
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MridulSamadder
{
    public partial class EmployeeInformationUI : Form
    {
        int globalListViewIndex;
        private List<Employee> employeeList = null;
        ListViewItem listviewItemObj = null;
        public EmployeeInformationUI()
        {
            employeeList = new List<Employee> ();
            InitializeComponent();
            FillComboWithData();          
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            Employee employeeObj = new Employee();
            employeeObj.Name = nameTextBox.Text;
            employeeObj.Id = idTextBox.Text;
            employeeObj.Designation = designationComboBox.SelectedValue.ToString();
            employeeObj.DateOfBirth = Convert.ToDateTime(dateOfBirthDateTimePicker.Text);
            employeeList.Add(employeeObj);
            PopulateListView(employeeObj);
          
        }
        public void PopulateListView(Employee employeeObj)
        {                          
                    listviewItemObj = new ListViewItem(employeeObj.Id);
                    listviewItemObj.SubItems.Add(employeeObj.Name);
                    listviewItemObj.SubItems.Add(employeeObj.Designation);
                    listviewItemObj.SubItems.Add(employeeObj.DateOfBirth.ToString());
                    employeeInformationListView.Items.Add(listviewItemObj);
       
         }
        public void FillComboWithData()
        {
            designationComboBox.DataSource = Enum.GetNames(typeof(DesignationType));
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            employeeInformationListView.Items.Remove(employeeInformationListView.Items[globalListViewIndex]);          
        }
     
        private void employeeInformationListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            globalListViewIndex = e.ItemIndex;          
        }     
        private void showButton_Click(object sender, EventArgs e)
        {
            string id = employeeInformationListView.Items[globalListViewIndex].Text;
            string name = employeeInformationListView.Items[globalListViewIndex].SubItems[1].Text;
            MessageBox.Show("Name Of The Employee:" + name);
        }

        private void EmployeeInformationUI_Load(object sender, EventArgs e)
        {

        }
    }
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger