⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Winform_理论部分 Wi培训机构的案例的理论部分
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataGridTest
{
	public class frmForm:System.Windows.Forms.Form 
	{
		private System.Windows.Forms.DataGrid dbgNewGrid;
		private DataTable tbSourceTable;
		private System.Windows.Forms.Button cmdFocus;
		private System.ComponentModel.Container components = null;
		public frmForm()
		{
			InitializeComponent();
			PopulateGrid();
		}
		protected override void Dispose( bool disposing )
		{
			if ( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码

		private void InitializeComponent()
		{
			this.dbgNewGrid = new System.Windows.Forms.DataGrid();
			this.cmdFocus = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dbgNewGrid)).BeginInit();
			this.SuspendLayout();
			this.dbgNewGrid.DataMember = "";
			this.dbgNewGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dbgNewGrid.Location = new System.Drawing.Point(4, 8);
			this.dbgNewGrid.Name = "dbgNewGrid";
			this.dbgNewGrid.Size = new System.Drawing.Size(316, 168);
			this.dbgNewGrid.TabIndex = 0;
			this.cmdFocus.Location = new System.Drawing.Point(232, 188);
			this.cmdFocus.Name = "cmdFocus";
			this.cmdFocus.Size = new System.Drawing.Size(84, 23);
			this.cmdFocus.TabIndex = 1;
			this.cmdFocus.Text = "获取焦点";
			this.cmdFocus.Click += new System.EventHandler(this.cmdFocus_Click);
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(332, 217);
			this.Controls.Add(this.cmdFocus);
			this.Controls.Add(this.dbgNewGrid);
			this.Name = "frmForm1";
			this.Text = "frmForm1";
			((System.ComponentModel.ISupportInitialize)(this.dbgNewGrid)).EndInit();
			this.ResumeLayout(false);
			/////////以上为拖放控件自动产生的代码//////////////////////////////////////////////////////////////
		}
		#endregion
		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmForm());
		}

		private void PopulateGrid()
		{
			///创建填充数据的表结构
			tbSourceTable = new DataTable ("table_Person");
			string[] strTitles = new string [3]{"姓名","学历","职务"};
			DataColumn colTextColumn = null;
			//创建前面三列,全部是字符串类型,第一列列名为"姓名",第二列为“学历”,第三列“职务”
			for(int i=0;i<3;i++)
			{
				colTextColumn = new DataColumn(strTitles[i]);
				colTextColumn.DataType = Type.GetType("System.String");
				colTextColumn.DefaultValue = "";
				tbSourceTable.Columns.Add(colTextColumn); 
			}
			//创建第四列,Bool类型,列名"在职"
			DataColumn colBoolColumn = new DataColumn("在职");
			colBoolColumn.DataType = System.Type.GetType("System.Boolean");
			colBoolColumn.DefaultValue = false;
			tbSourceTable.Columns.Add(colBoolColumn);

			///创建表结构结束
			
			dbgNewGrid.DataSource = tbSourceTable; //指定DataGrid数据源
			if(!dbgNewGrid.TableStyles.Contains("Style_Person"))//为定义的DataGrid取名"Style_Person"
			{
				//定制DataGridTableStyle,用于整个DataGrid
				DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
				dgdtblStyle.MappingName = tbSourceTable.TableName;
				dbgNewGrid.TableStyles.Add(dgdtblStyle);
				dgdtblStyle.RowHeadersVisible = false;
				dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;
				dgdtblStyle.AllowSorting = false;
				dgdtblStyle.HeaderBackColor = Color.FromArgb(8,36,107);
				dgdtblStyle.RowHeadersVisible = false;
				dgdtblStyle.HeaderForeColor = Color.White;
				dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, 
					System.Drawing.FontStyle.Bold, 
					System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
				dgdtblStyle.GridLineColor = Color.DarkGray;
				dgdtblStyle.PreferredRowHeight = 22;
				dbgNewGrid.BackgroundColor = Color.White;
				//定制DataGridTableStyle结束
				//通过GridColumnStyles设置列属性,以下设置列的宽度 
				GridColumnStylesCollection colStyle = dbgNewGrid.TableStyles[0].GridColumnStyles;
				colStyle[0].Width = 50;//姓名
				colStyle[1].Width = 100;//学历
				colStyle[2].Width = 50;//职务
				colStyle[3].Width = 40;//是否在职
			}
			//下面定制“学历”列,这里若要定制其他列可以修改GridColumnStyles[1]中的数值
			DataGridTextBoxColumn dgTextBoxColumn = (DataGridTextBoxColumn)dbgNewGrid.TableStyles[0].GridColumnStyles[1]; 
			//定制为ComboBox类型,若想定义成其他类型的可以在这里定制
			ComboBox dgComboBoxColumn = new ComboBox();
			dgComboBoxColumn.Items.AddRange(new object[]{"本科","硕士","博士"});//添加选项
			dgComboBoxColumn.Cursor = Cursors.Arrow;
			dgComboBoxColumn.DropDownStyle= ComboBoxStyle.DropDownList;
			dgComboBoxColumn.Dock = DockStyle.Fill;
			///在选定项发生更改并且提交了该更改后发生

			dgComboBoxColumn.SelectionChangeCommitted += new  EventHandler(dgComboBoxColumn_SelectionChangeCommitted); 
			///把ComboBox添加到DataGridTableStyle的第一列
			///在原来微软默认的TextBox控件里面添加入你所定义的控件
			dgTextBoxColumn.TextBox.Controls.Add(dgComboBoxColumn); 

		}

		//设置焦点模拟

		private void GetFocus(int row,int col)
		{
			///先把焦点移动到DataGrid
			this.dbgNewGrid.Focus(); 
			// 把焦点移动到DataGridCell
			DataGridCell dgCell = new DataGridCell(row,col); 
			this.dbgNewGrid.CurrentCell = dgCell; 
			DataGridTextBoxColumn dgTextBoxColumn = (DataGridTextBoxColumn)dbgNewGrid.TableStyles[0].GridColumnStyles[col]; 
			///设置焦点
			dgTextBoxColumn.TextBox.Focus();
		} 
		//把Combobox上修改的数据提交到当前的网格

		private  void dgComboBoxColumn_SelectionChangeCommitted (object sender, EventArgs e  )
		{
			dbgNewGrid[this.dbgNewGrid.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();


		} 
		///设置新的焦点
		private  void cmdFocus_Click(object sender, System.EventArgs e)
		{
			//焦点模拟,这里设置第三行第一列
			GetFocus(2,0);
		}
	}

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -