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;