WinForm DataGridView 的使用、与使用 ComboBox

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.13.6
.NET Framework 4.8

使用场景

BindingSource 组件可以跨窗体共享绑定数据

  1. 基于本地数据库模式
    定义顺序为:DataSet->BindingSource(绑定到DataSet的数据库和表)->DataGridView->BindingNavigator
  2. 基于前后端分离模式
    定义顺序为: DataSet(可以不定义,通过手动指定的防止给 BindingSource 赋值 DataSource )>BindingSource->DataGridView->BindingNavigator(绑定BindingSource组件)
    基于api形式也可以这么说BindingSource(将结构化数据数据赋值给DataSource)->DataGridView->BindingNavigator

正文

基于本地数据库模式

  1. 页面添加 DataSet 控件,添加时会提示选择添加类型化数据集(连接数据库)或非类型化数据集(不需要连接数据库),选择类型化数据集,然后添加对应的数据库连接即可,添加后会在控件页面底部位置出现一个你的数据库连接的DataSet
    image

  2. 添加 BindingSource 组件。
    image

    1. 属性DataSource指定为已添加的DataSet
    2. 属性DataMember指定为已添加DataSet内的数据表(指定后会自动添加一个对应数据表名TableAdapter控件)
  3. 添加 DataGridView 组件,并指定数据源为 BindingSource 组件(指定后字段自动填充,如果需要调整请点击组件的编辑列添加列
    image

  4. 添加 BindingNavigator 组件
    image

    1. 属性 DataSource指定为已添加的 BindingSource
  5. 点击运行程序,即可正常运行,相关操作也可以正常关联使用,如:添加、删除、修改、上一条、下一条。。(注意:1.记得启动当前连接的数据库,否则窗体不显示; 2.数据操作并没有入库,需要手动添加事件处理
    image

基于前后端分离模式

基于前后端分离模式可以不添加DataSet 控件。
添加也没问题,添加类型为非类型化数据集,手动维护数据表名、数据表的字段。
然后用DataSet的来管理DataGridView显示的列。(如果添加了DataSet,记得给属性DataMember指定为已添加DataSet内的数据表)

  1. 添加 BindingSource 组件
  2. 添加 DataGridView 组件
    1. 手动添加任意列,给每列设置DataPropertyName对应数据的字段名(不添加数据列会导致列表有数据也不显示
    2. 指定数据源为 BindingSource 组件
  3. 添加 BindingNavigator 组件,并指定数据源为 BindingSource 组件
  4. 代码中手动给BindingSourceDataSource属性赋值List类型数据
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 数据组件使用Demo
    {
    	public partial class Form2 : Form
    	{
    		public Form2()
    		{
    			InitializeComponent();
    			//this.bindingNavigator1.BindingSource = this.bindingSource1;
    			this.bindingSource1.DataSource = users;
    		}
    		private List<UserModel> users = new List<UserModel>() {
    			new UserModel()
    			{
    				Id = 1,
    				UserName="张三",
    				Sex=1,
    				Age=18
    			},
    			new UserModel()
    			{
    				Id = 2,
    				UserName="李四",
    				Sex=2,
    				Age=22
    			},
    			new UserModel()
    			{
    				Id =3,
    				UserName="王五",
    				Sex=2,
    				Age=30
    			},
    			new UserModel()
    			{
    				Id = 4,
    				UserName="赵六",
    				Sex=1,
    				Age=25
    			},
    		};
    
    		private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    		{
    			////e.ColumnIndex
    			//if (this.dataSet1.Tables == null)
    			//{
    			//    Debug.WriteLine(this.dataSet1.Tables["Students"].GetChanges());
    			//}
    
    
    		}
    	}
    	public class UserModel
    	{
    		public int Id { get; set; }
    		public string UserName { get; set; }
    		public int Sex { get; set; }
    		public int Age { get; set; }
    	}
    }
    
    
  5. 点击运行程序,即可正常运行,相关操作也可以正常关联使用,如:添加、删除、修改、上一条、下一条。。(注意:1.记得启动当前连接的数据库,否则窗体不显示; 2.数据操作并没有入库,需要手动添加事件处理

扩展

DataGridView 中使用 ComboBox

对应需要使用ComboBox的列,需要在 DataGridView->编辑列->Column修改为DataGridViewComboBoxColumn
image

  1. Form4.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 数据组件使用Demo
    {
    	public partial class Form4 : Form
    	{
    		public class ComboItem
    		{
    			public int Id { get; set; }
    			public string Text { get; set; }
    		}
    		private List<UserModel> users = new List<UserModel>() {
    			new UserModel()
    			{
    				Id = 1,
    				UserName="张三",
    				Sex=1,
    				Age=18
    			},
    			new UserModel()
    			{
    				Id = 2,
    				UserName="李四",
    				Sex=2,
    				Age=22
    			},
    			new UserModel()
    			{
    				Id =3,
    				UserName="王五",
    				Sex=2,
    				Age=30
    			},
    			new UserModel()
    			{
    				Id = 4,
    				UserName="赵六",
    				Sex=1,
    				Age=25
    			},
    		};
    
    		public Form4()
    		{
    			InitializeComponent();
    			//
    			this.bindingSource1.DataSource = users;
    			DataGridViewComboBoxColumn column = (DataGridViewComboBoxColumn)this.dataGridView1.Columns[2];
    			column.DataSource = new List<ComboItem>() {
    				new ComboItem(){ Id=1, Text ="男人" },
    				new ComboItem(){ Id=2, Text ="女人" },
    				new ComboItem(){ Id=3, Text ="不告诉你" },
    
    			};
    			column.DisplayMember = "Text";
    			column.ValueMember = "Id";
    		}
    	}
    }
    
    
  2. Form4.Designer.cs
    namespace 数据组件使用Demo
    {
    	partial class Form4
    	{
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.IContainer components = null;
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    		protected override void Dispose(bool disposing)
    		{
    			if (disposing && (components != null))
    			{
    				components.Dispose();
    			}
    			base.Dispose(disposing);
    		}
    
    		#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.components = new System.ComponentModel.Container();
    			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form4));
    			this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
    			this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
    			this.dataGridView1 = new System.Windows.Forms.DataGridView();
    			this.bindingNavigator1 = new System.Windows.Forms.BindingNavigator(this.components);
    			this.bindingNavigatorMoveFirstItem = new System.Windows.Forms.ToolStripButton();
    			this.bindingNavigatorMovePreviousItem = new System.Windows.Forms.ToolStripButton();
    			this.bindingNavigatorSeparator = new System.Windows.Forms.ToolStripSeparator();
    			this.bindingNavigatorPositionItem = new System.Windows.Forms.ToolStripTextBox();
    			this.bindingNavigatorCountItem = new System.Windows.Forms.ToolStripLabel();
    			this.bindingNavigatorSeparator1 = new System.Windows.Forms.ToolStripSeparator();
    			this.bindingNavigatorMoveNextItem = new System.Windows.Forms.ToolStripButton();
    			this.bindingNavigatorMoveLastItem = new System.Windows.Forms.ToolStripButton();
    			this.bindingNavigatorSeparator2 = new System.Windows.Forms.ToolStripSeparator();
    			this.bindingNavigatorAddNewItem = new System.Windows.Forms.ToolStripButton();
    			this.bindingNavigatorDeleteItem = new System.Windows.Forms.ToolStripButton();
    			this.Id = new System.Windows.Forms.DataGridViewTextBoxColumn();
    			this.UserName = new System.Windows.Forms.DataGridViewTextBoxColumn();
    			this.Sex = new System.Windows.Forms.DataGridViewComboBoxColumn();
    			this.printDocument1 = new System.Drawing.Printing.PrintDocument();
    			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
    			this.flowLayoutPanel1.SuspendLayout();
    			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.bindingNavigator1)).BeginInit();
    			this.bindingNavigator1.SuspendLayout();
    			this.SuspendLayout();
    			// 
    			// flowLayoutPanel1
    			// 
    			this.flowLayoutPanel1.Controls.Add(this.dataGridView1);
    			this.flowLayoutPanel1.Controls.Add(this.bindingNavigator1);
    			this.flowLayoutPanel1.Location = new System.Drawing.Point(239, 52);
    			this.flowLayoutPanel1.Name = "flowLayoutPanel1";
    			this.flowLayoutPanel1.Size = new System.Drawing.Size(358, 259);
    			this.flowLayoutPanel1.TabIndex = 0;
    			// 
    			// dataGridView1
    			// 
    			this.dataGridView1.AutoGenerateColumns = false;
    			this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    			this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    			this.Id,
    			this.UserName,
    			this.Sex});
    			this.dataGridView1.DataSource = this.bindingSource1;
    			this.dataGridView1.Location = new System.Drawing.Point(3, 3);
    			this.dataGridView1.Name = "dataGridView1";
    			this.dataGridView1.RowTemplate.Height = 23;
    			this.dataGridView1.Size = new System.Drawing.Size(355, 150);
    			this.dataGridView1.TabIndex = 0;
    			// 
    			// bindingNavigator1
    			// 
    			this.bindingNavigator1.AddNewItem = this.bindingNavigatorAddNewItem;
    			this.bindingNavigator1.BindingSource = this.bindingSource1;
    			this.bindingNavigator1.CountItem = this.bindingNavigatorCountItem;
    			this.bindingNavigator1.DeleteItem = this.bindingNavigatorDeleteItem;
    			this.bindingNavigator1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    			this.bindingNavigatorMoveFirstItem,
    			this.bindingNavigatorMovePreviousItem,
    			this.bindingNavigatorSeparator,
    			this.bindingNavigatorPositionItem,
    			this.bindingNavigatorCountItem,
    			this.bindingNavigatorSeparator1,
    			this.bindingNavigatorMoveNextItem,
    			this.bindingNavigatorMoveLastItem,
    			this.bindingNavigatorSeparator2,
    			this.bindingNavigatorAddNewItem,
    			this.bindingNavigatorDeleteItem});
    			this.bindingNavigator1.Location = new System.Drawing.Point(0, 156);
    			this.bindingNavigator1.MoveFirstItem = this.bindingNavigatorMoveFirstItem;
    			this.bindingNavigator1.MoveLastItem = this.bindingNavigatorMoveLastItem;
    			this.bindingNavigator1.MoveNextItem = this.bindingNavigatorMoveNextItem;
    			this.bindingNavigator1.MovePreviousItem = this.bindingNavigatorMovePreviousItem;
    			this.bindingNavigator1.Name = "bindingNavigator1";
    			this.bindingNavigator1.PositionItem = this.bindingNavigatorPositionItem;
    			this.bindingNavigator1.Size = new System.Drawing.Size(283, 25);
    			this.bindingNavigator1.TabIndex = 1;
    			this.bindingNavigator1.Text = "bindingNavigator1";
    			// 
    			// bindingNavigatorMoveFirstItem
    			// 
    			this.bindingNavigatorMoveFirstItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
    			this.bindingNavigatorMoveFirstItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveFirstItem.Image")));
    			this.bindingNavigatorMoveFirstItem.Name = "bindingNavigatorMoveFirstItem";
    			this.bindingNavigatorMoveFirstItem.RightToLeftAutoMirrorImage = true;
    			this.bindingNavigatorMoveFirstItem.Size = new System.Drawing.Size(23, 22);
    			this.bindingNavigatorMoveFirstItem.Text = "移到第一条记录";
    			// 
    			// bindingNavigatorMovePreviousItem
    			// 
    			this.bindingNavigatorMovePreviousItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
    			this.bindingNavigatorMovePreviousItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMovePreviousItem.Image")));
    			this.bindingNavigatorMovePreviousItem.Name = "bindingNavigatorMovePreviousItem";
    			this.bindingNavigatorMovePreviousItem.RightToLeftAutoMirrorImage = true;
    			this.bindingNavigatorMovePreviousItem.Size = new System.Drawing.Size(23, 22);
    			this.bindingNavigatorMovePreviousItem.Text = "移到上一条记录";
    			// 
    			// bindingNavigatorSeparator
    			// 
    			this.bindingNavigatorSeparator.Name = "bindingNavigatorSeparator";
    			this.bindingNavigatorSeparator.Size = new System.Drawing.Size(6, 25);
    			// 
    			// bindingNavigatorPositionItem
    			// 
    			this.bindingNavigatorPositionItem.AccessibleName = "位置";
    			this.bindingNavigatorPositionItem.AutoSize = false;
    			this.bindingNavigatorPositionItem.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F);
    			this.bindingNavigatorPositionItem.Name = "bindingNavigatorPositionItem";
    			this.bindingNavigatorPositionItem.Size = new System.Drawing.Size(50, 23);
    			this.bindingNavigatorPositionItem.Text = "0";
    			this.bindingNavigatorPositionItem.ToolTipText = "当前位置";
    			// 
    			// bindingNavigatorCountItem
    			// 
    			this.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem";
    			this.bindingNavigatorCountItem.Size = new System.Drawing.Size(32, 22);
    			this.bindingNavigatorCountItem.Text = "/ {0}";
    			this.bindingNavigatorCountItem.ToolTipText = "总项数";
    			// 
    			// bindingNavigatorSeparator1
    			// 
    			this.bindingNavigatorSeparator1.Name = "bindingNavigatorSeparator";
    			this.bindingNavigatorSeparator1.Size = new System.Drawing.Size(6, 25);
    			// 
    			// bindingNavigatorMoveNextItem
    			// 
    			this.bindingNavigatorMoveNextItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
    			this.bindingNavigatorMoveNextItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveNextItem.Image")));
    			this.bindingNavigatorMoveNextItem.Name = "bindingNavigatorMoveNextItem";
    			this.bindingNavigatorMoveNextItem.RightToLeftAutoMirrorImage = true;
    			this.bindingNavigatorMoveNextItem.Size = new System.Drawing.Size(23, 22);
    			this.bindingNavigatorMoveNextItem.Text = "移到下一条记录";
    			// 
    			// bindingNavigatorMoveLastItem
    			// 
    			this.bindingNavigatorMoveLastItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
    			this.bindingNavigatorMoveLastItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveLastItem.Image")));
    			this.bindingNavigatorMoveLastItem.Name = "bindingNavigatorMoveLastItem";
    			this.bindingNavigatorMoveLastItem.RightToLeftAutoMirrorImage = true;
    			this.bindingNavigatorMoveLastItem.Size = new System.Drawing.Size(23, 22);
    			this.bindingNavigatorMoveLastItem.Text = "移到最后一条记录";
    			// 
    			// bindingNavigatorSeparator2
    			// 
    			this.bindingNavigatorSeparator2.Name = "bindingNavigatorSeparator";
    			this.bindingNavigatorSeparator2.Size = new System.Drawing.Size(6, 25);
    			// 
    			// bindingNavigatorAddNewItem
    			// 
    			this.bindingNavigatorAddNewItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
    			this.bindingNavigatorAddNewItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorAddNewItem.Image")));
    			this.bindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem";
    			this.bindingNavigatorAddNewItem.RightToLeftAutoMirrorImage = true;
    			this.bindingNavigatorAddNewItem.Size = new System.Drawing.Size(23, 22);
    			this.bindingNavigatorAddNewItem.Text = "新添";
    			// 
    			// bindingNavigatorDeleteItem
    			// 
    			this.bindingNavigatorDeleteItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
    			this.bindingNavigatorDeleteItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorDeleteItem.Image")));
    			this.bindingNavigatorDeleteItem.Name = "bindingNavigatorDeleteItem";
    			this.bindingNavigatorDeleteItem.RightToLeftAutoMirrorImage = true;
    			this.bindingNavigatorDeleteItem.Size = new System.Drawing.Size(23, 22);
    			this.bindingNavigatorDeleteItem.Text = "删除";
    			// 
    			// Id
    			// 
    			this.Id.DataPropertyName = "Id";
    			this.Id.HeaderText = "Id";
    			this.Id.Name = "Id";
    			// 
    			// UserName
    			// 
    			this.UserName.DataPropertyName = "UserName";
    			this.UserName.HeaderText = "UserName";
    			this.UserName.Name = "UserName";
    			// 
    			// Sex
    			// 
    			this.Sex.DataPropertyName = "Sex";
    			this.Sex.HeaderText = "Sex";
    			this.Sex.Name = "Sex";
    			// 
    			// Form4
    			// 
    			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    			this.ClientSize = new System.Drawing.Size(800, 450);
    			this.Controls.Add(this.flowLayoutPanel1);
    			this.Name = "Form4";
    			this.Text = "Form4";
    			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
    			this.flowLayoutPanel1.ResumeLayout(false);
    			this.flowLayoutPanel1.PerformLayout();
    			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.bindingNavigator1)).EndInit();
    			this.bindingNavigator1.ResumeLayout(false);
    			this.bindingNavigator1.PerformLayout();
    			this.ResumeLayout(false);
    
    		}
    
    		#endregion
    
    		private System.Windows.Forms.BindingSource bindingSource1;
    		private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
    		private System.Windows.Forms.DataGridView dataGridView1;
    		private System.Windows.Forms.BindingNavigator bindingNavigator1;
    		private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;
    		private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;
    		private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;
    		private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;
    		private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;
    		private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;
    		private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;
    		private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;
    		private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;
    		private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;
    		private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;
    		private System.Windows.Forms.DataGridViewTextBoxColumn Id;
    		private System.Windows.Forms.DataGridViewTextBoxColumn UserName;
    		private System.Windows.Forms.DataGridViewComboBoxColumn Sex;
    		private System.Drawing.Printing.PrintDocument printDocument1;
    	}
    }
    
posted @ 2025-05-19 22:13  夏秋初  阅读(204)  评论(0)    收藏  举报