C# 开发Winform DataGridView的增删改查实战

在C# WinForms应用程序中,DataGridView控件是一个非常强大的工具,用于显示和编辑表格数据。下面我将详细介绍如何在WinForm应用程序中使用DataGridView实现基本的数据库操作:增加、删除、修改和查询(CRUD)。

第一步:创建数据库表

首先,你需要一个数据库表来存储数据。假设我们有一个"Employee"表,包含以下字段:

  • ID (int, primary key, auto-increment)
  • Name (varchar)
  • Position (varchar)
  • Salary (decimal)

第二步:设置WinForm项目

  1. 打开Visual Studio,创建一个新的Windows Forms App (.NET Framework)项目。
  2. 在Form上添加一个DataGridView控件。

第三步:连接数据库

使用ADO.NET来连接数据库。在解决方案资源管理器中,右键点击项目 -> 添加 -> 新建项 -> 数据 -> ADO.NET实体数据模型,然后选择你的数据库并创建实体。

第四步:编写CRUD操作

1. 查询数据
复制代码

Csharp

深色版本

1private void LoadData()
2{
3    using (var context = new YourDbContext())
4    {
5        var employees = context.Employees.ToList();
6        dataGridView1.DataSource = employees;
7    }
8}
2. 插入数据
复制代码

Csharp

1private void InsertData(string name, string position, decimal salary)
2{
3    using (var context = new YourDbContext())
4    {
5        var employee = new Employee { Name = name, Position = position, Salary = salary };
6        context.Employees.Add(employee);
7        context.SaveChanges();
8        LoadData(); // 刷新DataGridView
9    }
10}
3. 更新数据
复制代码

Csharp

1private void UpdateData(int id, string name, string position, decimal salary)
2{
3    using (var context = new YourDbContext())
4    {
5        var employee = context.Employees.Find(id);
6        if (employee != null)
7        {
8            employee.Name = name;
9            employee.Position = position;
10            employee.Salary = salary;
11            context.SaveChanges();
12            LoadData(); // 刷新DataGridView
13        }
14    }
15}
4. 删除数据
复制代码

Csharp

1private void DeleteData(int id)
2{
3    using (var context = new YourDbContext())
4    {
5        var employee = context.Employees.Find(id);
6        if (employee != null)
7        {
8            context.Employees.Remove(employee);
9            context.SaveChanges();
10            LoadData(); // 刷新DataGridView
11        }
12    }
13}

第五步:绑定事件

在你的Form中,你可以绑定DataGridView的事件来触发上述方法,例如:

复制代码

Csharp

1private void buttonAdd_Click(object sender, EventArgs e)
2{
3    InsertData(textBoxName.Text, textBoxPosition.Text, decimal.Parse(textBoxSalary.Text));
4}
5
6private void buttonEdit_Click(object sender, EventArgs e)
7{
8    var selectedRow = dataGridView1.SelectedRows[0];
9    int id = Convert.ToInt32(selectedRow.Cells["ID"].Value);
10    string name = selectedRow.Cells["Name"].Value.ToString();
11    string position = selectedRow.Cells["Position"].Value.ToString();
12    decimal salary = Convert.ToDecimal(selectedRow.Cells["Salary"].Value);
13    UpdateData(id, name, position, salary);
14}
15
16private void buttonDelete_Click(object sender, EventArgs e)
17{
18    var selectedRow = dataGridView1.SelectedRows[0];
19    int id = Convert.ToInt32(selectedRow.Cells["ID"].Value);
20    DeleteData(id);
21}

第六步:运行程序

现在,你可以运行你的应用程序,通过按钮来添加、编辑和删除数据了。记得在启动时调用LoadData()方法来初始化DataGridView的数据。

以上步骤和代码提供了一个基础框架,你可以根据具体需求进行调整和扩展。例如,你可能需要添加错误处理、数据验证等。

相关推荐
豆豆18 分钟前
为什么用PageAdmin CMS建设网站?
服务器·开发语言·前端·php·软件构建
bug菌¹41 分钟前
滚雪球学Oracle[2.5讲]:数据库初始化配置
数据库·oracle·数据库初始化·初始化配置
落落落sss1 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
简单.is.good1 小时前
【测试】接口测试与接口自动化
开发语言·python
Yvemil71 小时前
MQ 架构设计原理与消息中间件详解(二)
开发语言·后端·ruby
程序员是干活的1 小时前
私家车开车回家过节会发生什么事情
java·开发语言·软件构建·1024程序员节
我是陈泽2 小时前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
优雅的小武先生2 小时前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
明耀2 小时前
WPF RadioButton 绑定boolean值
c#·wpf
虽千万人 吾往矣2 小时前
golang gorm
开发语言·数据库·后端·tcp/ip·golang