【Winform使用DataGridView实现表格数据的添加,编辑、删除、分页功能】

news/2024/11/6 10:24:39/

Winform使用DataGridView实现表格数据的添加,编辑、删除、分页功能。

一、效果预览

在这里插入图片描述

二、代码

Form1

public partial class Form1 : Form{private BindingSource bindingSource = new BindingSource();private List<Student> students = new List<Student>();private int currentPage = 0;private int pageSize = 10;public Form1(){InitializeComponent();CenterToParent();CenterToScreen();LoadData();// 设置分页bindingSource.DataSource = GetPagedData(0, pageSize);dataGridView1.DataSource = bindingSource;// 添加编辑按钮列DataGridViewButtonColumn editButtonColumn = new DataGridViewButtonColumn();editButtonColumn.Name = "Edit";editButtonColumn.HeaderText = "编辑";editButtonColumn.Text = "编辑";editButtonColumn.UseColumnTextForButtonValue = true;dataGridView1.Columns.Add(editButtonColumn);// 添加删除按钮列DataGridViewButtonColumn deleteButtonColumn = new DataGridViewButtonColumn();deleteButtonColumn.Name = "Delete";deleteButtonColumn.HeaderText = "删除";deleteButtonColumn.Text = "删除";deleteButtonColumn.UseColumnTextForButtonValue = true;dataGridView1.Columns.Add(deleteButtonColumn);Label_CurrentPageShow.Text = $"{currentPage+1}/{students.Count / 10}";}private void Form1_Load(object sender, EventArgs e){ }private void LoadData(){// 假设我们有一些数据for (int i = 1; i <= 100; i++){string id = (10000+i).ToString();students.Add(new Student { ID = id, Name = "Name" + i, Major = "Major" + i });}}private List<Student> GetPagedData(int pageIndex, int pageSize){return students.Skip(pageIndex * pageSize).Take(pageSize).ToList();}private void btnPrevious_Click(object sender, EventArgs e){if (currentPage > 0){currentPage--;bindingSource.DataSource = GetPagedData(currentPage, pageSize);ShowPageNumber();}}private void btnNext_Click(object sender, EventArgs e){if ((currentPage + 1) * pageSize < students.Count){currentPage++;bindingSource.DataSource = GetPagedData(currentPage, pageSize);ShowPageNumber();}}private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){if (e.RowIndex >= 0){// 获取当前行的数据var student = (Student)bindingSource[e.RowIndex];if (e.ColumnIndex == dataGridView1.Columns["Edit"].Index){// 编辑操作EditStudent(student);}else if (e.ColumnIndex == dataGridView1.Columns["Delete"].Index){// 删除操作DeleteStudent(student);}}}private void EditStudent(Student student){// 编辑逻辑,例如弹出一个对话框让用户编辑信息MessageBox.Show($"编辑: {student.Name}");}private void DeleteStudent(Student student){if (MessageBox.Show($"删除用户:{student.Name}", "确认删除?", MessageBoxButtons.YesNo)== DialogResult.Yes){// 删除逻辑students.Remove(student);bindingSource.DataSource = GetPagedData(currentPage, pageSize);MessageBox.Show($"删除成功: {student.Name}");}}InputBox inputBox = new InputBox();private void Btn_Add_Click(object sender, EventArgs e){if (inputBox.OnValueComfirmChanged==null){inputBox.OnValueComfirmChanged += ValueComfirmChangedCallback;}inputBox.Show();  }private void ValueComfirmChangedCallback(object sender, object receiver){if(receiver!=null &&  receiver is Student){Student student = (Student)receiver;student.ID = (10000 + students.Count).ToString();students.Add(student);ShowPageNumber();}inputBox.Hide();}public void ShowPageNumber(){int pageNum = 0;if (students.Count % 10>0){pageNum = students.Count / 10+1;}else{pageNum = students.Count / 10;}Label_CurrentPageShow.Text = $"{currentPage + 1}/{pageNum}";}}

Student

public class Student{public string ID { get; set; }public string Name { get; set; }public string Major { get; set; }public Student(){}public Student(string name ,string major){Name = name;Major = major;}}

InputBox

public delegate void ValueComfirmChanged(object sender ,object receiver);
public ValueComfirmChanged OnValueComfirmChanged;
public InputBox()
{InitializeComponent();this.CenterToParent();
}private void Btn_Comfirm_Click(object sender, EventArgs e)
{OnValueComfirmChanged?.Invoke(this, new Student(Tbx_Name.Text,Tbx_Major.Text));
}private void Btn_Cancel_Click(object sender, EventArgs e)
{Tbx_Name.Clear();Tbx_Major.Clear();this.Hide();
}

界面设计

InputBox界面

在这里插入图片描述

Form1界面

在这里插入图片描述

补充代码

Form1设计器代码

partial class Form1
{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.dataGridView1 = new System.Windows.Forms.DataGridView();this.panel1 = new System.Windows.Forms.Panel();this.Btn_Add = new System.Windows.Forms.Button();this.panel2 = new System.Windows.Forms.Panel();this.groupBox1 = new System.Windows.Forms.GroupBox();this.panel3 = new System.Windows.Forms.Panel();this.Btn_NextPage = new System.Windows.Forms.Button();this.Btn_UpPage = new System.Windows.Forms.Button();this.Label_CurrentPageShow = new System.Windows.Forms.Label();((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();this.panel1.SuspendLayout();this.panel2.SuspendLayout();this.groupBox1.SuspendLayout();this.panel3.SuspendLayout();this.SuspendLayout();// // dataGridView1// this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;this.dataGridView1.Location = new System.Drawing.Point(3, 24);this.dataGridView1.Name = "dataGridView1";this.dataGridView1.RowHeadersWidth = 62;this.dataGridView1.Size = new System.Drawing.Size(1079, 525);this.dataGridView1.TabIndex = 0;this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);// // panel1// this.panel1.Controls.Add(this.Btn_Add);this.panel1.Dock = System.Windows.Forms.DockStyle.Top;this.panel1.Location = new System.Drawing.Point(0, 0);this.panel1.Name = "panel1";this.panel1.Size = new System.Drawing.Size(1085, 100);this.panel1.TabIndex = 1;// // Btn_Add// this.Btn_Add.Location = new System.Drawing.Point(939, 48);this.Btn_Add.Name = "Btn_Add";this.Btn_Add.Size = new System.Drawing.Size(134, 46);this.Btn_Add.TabIndex = 2;this.Btn_Add.Text = "添加";this.Btn_Add.UseVisualStyleBackColor = true;this.Btn_Add.Click += new System.EventHandler(this.Btn_Add_Click);// // panel2// this.panel2.Controls.Add(this.groupBox1);this.panel2.Controls.Add(this.panel3);this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;this.panel2.Location = new System.Drawing.Point(0, 100);this.panel2.Name = "panel2";this.panel2.Size = new System.Drawing.Size(1085, 652);this.panel2.TabIndex = 2;// // groupBox1// this.groupBox1.Controls.Add(this.dataGridView1);this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;this.groupBox1.Location = new System.Drawing.Point(0, 0);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(1085, 552);this.groupBox1.TabIndex = 1;this.groupBox1.TabStop = false;this.groupBox1.Text = "groupBox1";// // panel3// this.panel3.Controls.Add(this.Label_CurrentPageShow);this.panel3.Controls.Add(this.Btn_NextPage);this.panel3.Controls.Add(this.Btn_UpPage);this.panel3.Dock = System.Windows.Forms.DockStyle.Bottom;this.panel3.Location = new System.Drawing.Point(0, 552);this.panel3.Name = "panel3";this.panel3.Size = new System.Drawing.Size(1085, 100);this.panel3.TabIndex = 2;// // Btn_NextPage// this.Btn_NextPage.Location = new System.Drawing.Point(626, 42);this.Btn_NextPage.Name = "Btn_NextPage";this.Btn_NextPage.Size = new System.Drawing.Size(134, 46);this.Btn_NextPage.TabIndex = 1;this.Btn_NextPage.Text = "下一页";this.Btn_NextPage.UseVisualStyleBackColor = true;this.Btn_NextPage.Click += new System.EventHandler(this.btnNext_Click);// // Btn_UpPage// this.Btn_UpPage.Location = new System.Drawing.Point(370, 42);this.Btn_UpPage.Name = "Btn_UpPage";this.Btn_UpPage.Size = new System.Drawing.Size(134, 46);this.Btn_UpPage.TabIndex = 0;this.Btn_UpPage.Text = "上一页";this.Btn_UpPage.UseVisualStyleBackColor = true;this.Btn_UpPage.Click += new System.EventHandler(this.btnPrevious_Click);// // Label_CurrentPageShow// this.Label_CurrentPageShow.AutoSize = true;this.Label_CurrentPageShow.Font = new System.Drawing.Font("宋体", 12F);this.Label_CurrentPageShow.Location = new System.Drawing.Point(530, 54);this.Label_CurrentPageShow.Name = "Label_CurrentPageShow";this.Label_CurrentPageShow.Size = new System.Drawing.Size(46, 24);this.Label_CurrentPageShow.TabIndex = 2;this.Label_CurrentPageShow.Text = "0/0";// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(1085, 752);this.Controls.Add(this.panel2);this.Controls.Add(this.panel1);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();this.panel1.ResumeLayout(false);this.panel2.ResumeLayout(false);this.groupBox1.ResumeLayout(false);this.panel3.ResumeLayout(false);this.panel3.PerformLayout();this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.DataGridView dataGridView1;private System.Windows.Forms.Panel panel1;private System.Windows.Forms.Panel panel2;private System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.Panel panel3;private System.Windows.Forms.Button Btn_UpPage;private System.Windows.Forms.Button Btn_NextPage;private System.Windows.Forms.Button Btn_Add;private System.Windows.Forms.Label Label_CurrentPageShow;
}

InputBox设计器代码

#region Windows Form Designer generated code/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{this.label1 = new System.Windows.Forms.Label();this.Tbx_Name = new System.Windows.Forms.TextBox();this.Tbx_Major = new System.Windows.Forms.TextBox();this.label2 = new System.Windows.Forms.Label();this.Btn_Comfirm = new System.Windows.Forms.Button();this.Btn_Cancel = new System.Windows.Forms.Button();this.SuspendLayout();// // label1// this.label1.AutoSize = true;this.label1.Font = new System.Drawing.Font("宋体", 12F);this.label1.Location = new System.Drawing.Point(12, 37);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(70, 24);this.label1.TabIndex = 0;this.label1.Text = "Name:";// // Tbx_Name// this.Tbx_Name.Font = new System.Drawing.Font("宋体", 12F);this.Tbx_Name.Location = new System.Drawing.Point(124, 34);this.Tbx_Name.Name = "Tbx_Name";this.Tbx_Name.Size = new System.Drawing.Size(216, 35);this.Tbx_Name.TabIndex = 1;// // Tbx_Major// this.Tbx_Major.Font = new System.Drawing.Font("宋体", 12F);this.Tbx_Major.Location = new System.Drawing.Point(124, 114);this.Tbx_Major.Name = "Tbx_Major";this.Tbx_Major.Size = new System.Drawing.Size(216, 35);this.Tbx_Major.TabIndex = 3;// // label2// this.label2.AutoSize = true;this.label2.Font = new System.Drawing.Font("宋体", 12F);this.label2.Location = new System.Drawing.Point(12, 117);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(82, 24);this.label2.TabIndex = 2;this.label2.Text = "Major:";// // Btn_Comfirm// this.Btn_Comfirm.Location = new System.Drawing.Point(69, 189);this.Btn_Comfirm.Name = "Btn_Comfirm";this.Btn_Comfirm.Size = new System.Drawing.Size(98, 44);this.Btn_Comfirm.TabIndex = 4;this.Btn_Comfirm.Text = "Comfirm";this.Btn_Comfirm.UseVisualStyleBackColor = true;this.Btn_Comfirm.Click += new System.EventHandler(this.Btn_Comfirm_Click);// // Btn_Cancel// this.Btn_Cancel.Location = new System.Drawing.Point(200, 189);this.Btn_Cancel.Name = "Btn_Cancel";this.Btn_Cancel.Size = new System.Drawing.Size(98, 44);this.Btn_Cancel.TabIndex = 5;this.Btn_Cancel.Text = "Cancel";this.Btn_Cancel.UseVisualStyleBackColor = true;this.Btn_Cancel.Click += new System.EventHandler(this.Btn_Cancel_Click);// // InputBox// this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(352, 262);this.Controls.Add(this.Btn_Cancel);this.Controls.Add(this.Btn_Comfirm);this.Controls.Add(this.Tbx_Major);this.Controls.Add(this.label2);this.Controls.Add(this.Tbx_Name);this.Controls.Add(this.label1);this.Name = "InputBox";this.Text = "InputBox";this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox Tbx_Name;
private System.Windows.Forms.TextBox Tbx_Major;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button Btn_Comfirm;
private System.Windows.Forms.Button Btn_Cancel;

http://www.ppmy.cn/news/1544793.html

相关文章

从0开始学习shell脚本

了解Shell和Shell脚本 Shell&#xff1a;Shell是一个命令解释器&#xff0c;用来执行用户输入的命令。常用的Shell包括Bash、Zsh、Ksh等。Linux默认的Shell通常是Bash。 Shell脚本&#xff1a;Shell脚本是由一系列命令组成的文件&#xff0c;脚本可以运行一连串命令&#xff…

PySpark 本地开发环境搭建与实践

目录 一、PySpark 本地开发环境搭建 &#xff08;一&#xff09;Windows 本地 JDK 和 Hadoop 的安装 &#xff08;二&#xff09;Windows 安装 Anaconda &#xff08;三&#xff09;Anaconda 中安装 PySpark &#xff08;四&#xff09;Pycharm 中创建工程 二、编写代码 …

WPF+MVVM案例实战与特效(二十六)- 3D粒子方块波浪墙效果实现

文章目录 1、案例效果2、案例实现1、文件创建2. 功能代码实现3、粒子功能应用1、前端布局与样式2、代码解释2、 后端功能代码1、案例效果 2、案例实现 1、文件创建 打开 Wpf_Examples 项目、Models 文件夹下创建 3D粒子模型类 ParticleCubeWaveModel.cs 文件。在Tools 文件夹…

Visual Studio2022版本的下载与安装

1-首先打开微软的官网&#xff0c;下面就是链接 下载 Visual Studio Tools - 免费安装 Windows、Mac、Linux免费下载 Visual Studio IDE 或 VS Code。 在 Windows、Mac 上试用 Visual Studio Professional 或企业版。https://visualstudio.microsoft.com/zh-hans/downloads/?…

针对告警数量、告警位置、告警类型等参数进行统计,并做可视化处理的智慧能源开源了。

一、简介 AI视频监控平台, 是一款功能强大且简单易用的实时算法视频监控系统。愿景在最底层打通各大芯片厂商相互间的壁垒&#xff0c;省去繁琐重复的适配流程&#xff0c;实现芯片、算法、应用的全流程组合&#xff0c;减少企业级应用约 95%的开发成本&#xff0c;在强大视频算…

sql中判断一个字段是否包含一个数据的方法

目录 一、使用LIKE关键字 示例 应用场景 二、使用IN关键字 示例 应用场景 三、使用FIND_IN_SET函数&#xff08;MySQL专用&#xff09; 示例 应用场景 四、使用CHARINDEX或POSITION函数 示例 应用场景 五、使用正则表达式&#xff08;部分数据库支持&#xff09; …

基于Spring Boot的智慧草莓基地管理系统设计与实现,LW+源码+讲解

摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本智慧草莓基地管理系统就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处理完毕庞大的数…

鉴源实验室·如何通过雷达攻击自动驾驶汽车-针对点云识别模型的对抗性攻击的科普

01 引 言 随着自动驾驶技术的迅速发展&#xff0c;雷达和激光雷达等传感器在自动驾驶汽车中的作用愈发重要。它们能够生成3D点云数据&#xff0c;帮助车辆实时感知周围环境并做出安全决策。然而&#xff0c;尽管这些传感器对驾驶环境的检测非常精确&#xff0c;它们也面临一种…