Creating a CRUD (Create, Read, Update, Delete) desktop application using Windows Forms in
C# involves several steps. Below is a guide to help you get started. For this example, we will
create a simple application that manages a list of books. Each book will have an ID, Title,
Author, and Year.
Prerequisites
1. Visual Studio: Ensure you have Visual Studio installed with the .NET desktop
development workload.
Steps to Create the CRUD Application
1. Create a New Project:
o Open Visual Studio.
o Click on "Create a new project".
o Select "Windows Forms App (.NET Framework)" and click "Next".
o Name your project (e.g., "BookManager") and click "Create".
2. Design the Form:
o In the Form1 designer, add controls to your form:
TextBoxes: For entering book details (ID, Title, Author, Year).
Buttons: For Create, Read, Update, and Delete operations.
DataGridView: For displaying the list of books.
3. Set Up the Controls:
o Name the TextBoxes (e.g., txtID, txtTitle, txtAuthor, txtYear).
o Name the Buttons (e.g., btnCreate, btnRead, btnUpdate, btnDelete).
o Name the DataGridView (e.g., dataGridViewBooks).
4. Add Event Handlers:
o Double-click each button to create event handlers in the code-behind.
5. Write the Code:
o Create a class to represent a book:
csharp
Copy code
public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public int Year { get; set; }
}
o Create a list to store the books:
csharp
Copy code
public partial class Form1 : Form
{
private List<Book> books = new List<Book>();
public Form1()
{
InitializeComponent();
}
// CRUD operations code here
}
o Implement the CRUD operations:
csharp
Copy code
private void btnCreate_Click(object sender, EventArgs e)
{
Book newBook = new Book
{
ID = int.Parse(txtID.Text),
Title = txtTitle.Text,
Author = txtAuthor.Text,
Year = int.Parse(txtYear.Text)
};
books.Add(newBook);
RefreshDataGrid();
}
private void btnRead_Click(object sender, EventArgs e)
{
RefreshDataGrid();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
int id = int.Parse(txtID.Text);
Book book = books.FirstOrDefault(b => b.ID == id);
if (book != null)
{
book.Title = txtTitle.Text;
book.Author = txtAuthor.Text;
book.Year = int.Parse(txtYear.Text);
RefreshDataGrid();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
int id = int.Parse(txtID.Text);
Book book = books.FirstOrDefault(b => b.ID == id);
if (book != null)
{
books.Remove(book);
RefreshDataGrid();
}
}
private void RefreshDataGrid()
{
dataGridViewBooks.DataSource = null;
dataGridViewBooks.DataSource = books;
}
6. Run the Application:
o Press F5 to run the application.
o You should be able to add, read, update, and delete books using the interface.
Summary
This basic example demonstrates how to create a CRUD application using Windows Forms in
C#. The application uses in-memory storage (a list of books), but in a real-world scenario, you
would typically connect to a database. For that, you could use ADO.NET or Entity Framework to
interact with a SQL database.