一、DataGridView控件及元素初始化
//DataGridView控件初始化 public System.Windows.Forms.DataGridView dataGridView1; this.dataGridView1 = new System.Windows.Forms.DataGridView(); //DataGridView列初始化 public System.Windows.Forms.DataGridViewCheckBoxColumn Column1; public System.Windows.Forms.DataGridViewTextBoxColumn Column2; public System.Windows.Forms.DataGridViewTextBoxColumn Column3; public System.Windows.Forms.DataGridViewTextBoxColumn Column4; public System.Windows.Forms.DataGridViewTextBoxColumn Column5; this.Column1 = new System.Windows.Forms.DataGridViewCheckBoxColumn(); this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn(); //右键菜单初始化 public System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
二、DataGridView控件参数设置
this.dataGridView1.AllowUserToResizeRows = false;//设置用户是否可以调整行的大小。 this.dataGridView1.BackgroundColor = System.Drawing.Color.White;//设置DataGridView控件背景色 this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;//设置右键菜单 this.dataGridView1.GridColor = System.Drawing.SystemColors.Control; this.dataGridView1.MultiSelect = false;//是否允许多行选择 this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;//设置垂直滚动条 this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;//设置选择模式为整行选择
三、DataGridViewColumn参数初始化
this.Column1.HeaderText = "选择"; this.Column1.Name = "Column1"; this.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.False; this.Column1.Width = 40; this.Column2.HeaderText = "名称"; this.Column2.Name = "Column2"; this.Column2.Resizable = System.Windows.Forms.DataGridViewTriState.False; this.Column2.Width = 280; this.Column3.HeaderText = "学科"; this.Column3.Name = "Column3"; this.Column3.Resizable = System.Windows.Forms.DataGridViewTriState.False; this.Column3.Width = 240; this.Column4.HeaderText = "级别"; this.Column4.Name = "Column4"; this.Column4.Resizable = System.Windows.Forms.DataGridViewTriState.False; this.Column4.Width = 60; this.Column5.HeaderText = "编码"; this.Column5.Name = "Column5"; this.Column5.Resizable = System.Windows.Forms.DataGridViewTriState.False;
四、添加DataGridViewColumn至DataGridView
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[{ this.Column1, this.Column2, this.Column3, this.Column4, this.Column5});
标签:控件,Windows,System,DataGridView,学习,dataGridView1,Forms,Column4 From: https://www.cnblogs.com/DingdingAnt/p/18508797