Create an Employee Directory
This time we will create an employee directory, a simple and yet fully functional application, which will allow you to add and store employee-related information.
So, fire up Visual Studio 2022, create a new project, choose a Windows Forms App written in C#, and then press the “Next” button.
Let’s name this project… “Employee Directory” – what else?
Click “Next”, choose “.NET 6.0 (Long Term Support)”, and then press the “Create” button. Visual Studio will do its thing, and then it will show you the empty form you’ve become accustomed to.
It’s time to create the user interface for our application. Drag and drop five labels, four TextBoxes, a Button and a DataGridView control onto the form.
Feel free to name the Labels the way you want them, but give the TextBoxes these property names: "txtName," "txtPhoneNumber," "txtEmail," and "txtDepartment."
Then, set the "Text" property of the Button control to "Add Employee"; keep its default name - button1. Finally, make sure that the DataGridView is named “dataGridView1”.
Here’s how my finalized form looks like.
It’s time to write the code for our project. This application is more complex, so there is quite a bit of code. I don’t want you to waste a lot of time trying to reproduce it from my screenshots, so here it is, ready to be copied/pasted into your project.
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace Employee_Directory
{
public partial class Form1 : Form
{
private List<Employee> employees = new List<Employee>();
private BindingSource bindingSource = new BindingSource();
private string filePath = "employees.xml";
public Form1()
{
InitializeComponent();
LoadEmployees();
InitializeDataGridView();
}
private void button1_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string phoneNumber = txtPhoneNumber.Text;
string email = txtEmail.Text;
string department = txtDepartment.Text;
Employee employee = new Employee
{
Name = name,
PhoneNumber = phoneNumber,
Email = email,
Department = department
};
employees.Add(employee);
SaveEmployees();
MessageBox.Show("Employee added successfully!");
ClearTextBoxes();
DisplayEmployees();
}
private void ClearTextBoxes()
{
txtName.Text = string.Empty;
txtPhoneNumber.Text = string.Empty;
txtEmail.Text = string.Empty;
txtDepartment.Text = string.Empty;
}
private void SaveEmployees()
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Employee>));
using (StreamWriter writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, employees);
}
}
catch (Exception ex)
{
MessageBox.Show("Error saving employees: " + ex.Message);
}
}
private void LoadEmployees()
{
if (File.Exists(filePath))
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Employee>));
using (StreamReader reader = new StreamReader(filePath))
{
employees = (List<Employee>)serializer.Deserialize(reader);
}
}
catch (Exception ex)
{
MessageBox.Show("Error loading employees: " + ex.Message);
}
}
}
private void InitializeDataGridView()
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = bindingSource;
DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
nameColumn.DataPropertyName = "Name";
nameColumn.HeaderText = "Name";
dataGridView1.Columns.Add(nameColumn);
DataGridViewTextBoxColumn phoneColumn = new DataGridViewTextBoxColumn();
phoneColumn.DataPropertyName = "PhoneNumber";
phoneColumn.HeaderText = "Phone Number";
dataGridView1.Columns.Add(phoneColumn);
DataGridViewTextBoxColumn emailColumn = new DataGridViewTextBoxColumn();
emailColumn.DataPropertyName = "Email";
emailColumn.HeaderText = "Email";
dataGridView1.Columns.Add(emailColumn);
DataGridViewTextBoxColumn departmentColumn = new DataGridViewTextBoxColumn();
departmentColumn.DataPropertyName = "Department";
departmentColumn.HeaderText = "Department";
dataGridView1.Columns.Add(departmentColumn);
}
private void DisplayEmployees()
{
bindingSource.DataSource = employees;
dataGridView1.Refresh();
}
}
public class Employee
{
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public string Department { get; set; }
}
}
Copy/paste the entire code inside Form1.cs, replacing the existing code. Then, build the project and run it; you will see a window that looks like the one below – if you try to reproduce my layout, of course.
Let’s add a new employee! Fill in the information in the TextBoxes, and then press the “Add Employee” button; if everything works as expected, you will see the data being added to the grid.
The code saves and retrieves employee data to/from an XML file, so the information won’t be lost after you close the program.
I hope you like this business application. There’s still work to do with it, but I think that it demonstrates my programming skills. If you need a custom software solution, let’s discuss it.