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

📄 test_info.cs

📁 学生管理系统
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace chaxun
{
	/// <summary>
	/// Test_info 的摘要说明。
	/// </summary>
	public class Test_info : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.DataGrid dataGrid1;
		private SqlDataAdapter thisAdapter;
		private DataSet mydataset;		
		private System.Windows.Forms.TextBox txtID;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.Button btnIncrease;
		private System.Windows.Forms.Button btnModification;
		private System.Windows.Forms.Button btndelete;
		private System.Windows.Forms.Button btnnext;
		private System.Windows.Forms.Button btnbefore;
		private System.Windows.Forms.Button button1;
		BindingManagerBase bManager;

		private System.Windows.Forms.CurrencyManager cmAmend;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		/// 
		
		private System.ComponentModel.Container components = null;

		public Test_info()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			SqlConnection thisConnection=new SqlConnection("integrated security=SSPI;data source=.;persist security info=False;initial catalog=xxgcx");  
			thisAdapter=new SqlDataAdapter("	SELECT Test_Id as 课程编号,Test_name as 课程名称 from test",thisConnection);
			SqlCommandBuilder thisBuilder=new SqlCommandBuilder(thisAdapter);
			mydataset=new DataSet();
			thisAdapter.Fill(mydataset,"test");
			
			

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label2 = new System.Windows.Forms.Label();
			this.txtID = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtName = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.btnIncrease = new System.Windows.Forms.Button();
			this.btnModification = new System.Windows.Forms.Button();
			this.btndelete = new System.Windows.Forms.Button();
			this.btnnext = new System.Windows.Forms.Button();
			this.btnbefore = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 26);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 23);
			this.label2.TabIndex = 1;
			this.label2.Text = "课程编号";
			// 
			// txtID
			// 
			this.txtID.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtID.Location = new System.Drawing.Point(96, 24);
			this.txtID.Name = "txtID";
			this.txtID.Size = new System.Drawing.Size(120, 25);
			this.txtID.TabIndex = 2;
			this.txtID.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(232, 26);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 23);
			this.label3.TabIndex = 3;
			this.label3.Text = "课程名称";
			// 
			// txtName
			// 
			this.txtName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtName.Location = new System.Drawing.Point(320, 24);
			this.txtName.Name = "txtName";
			this.txtName.Size = new System.Drawing.Size(136, 25);
			this.txtName.TabIndex = 4;
			this.txtName.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.txtID);
			this.groupBox1.Controls.Add(this.txtName);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Location = new System.Drawing.Point(16, 288);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(464, 56);
			this.groupBox1.TabIndex = 9;
			this.groupBox1.TabStop = false;
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(16, 0);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.PreferredColumnWidth = 210;
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.Size = new System.Drawing.Size(464, 288);
			this.dataGrid1.TabIndex = 10;
			// 
			// btnIncrease
			// 
			this.btnIncrease.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnIncrease.Location = new System.Drawing.Point(16, 360);
			this.btnIncrease.Name = "btnIncrease";
			this.btnIncrease.Size = new System.Drawing.Size(64, 29);
			this.btnIncrease.TabIndex = 11;
			this.btnIncrease.Text = "添加";
			this.btnIncrease.Click += new System.EventHandler(this.btnIncrease_Click);
			// 
			// btnModification
			// 
			this.btnModification.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnModification.Location = new System.Drawing.Point(88, 360);
			this.btnModification.Name = "btnModification";
			this.btnModification.Size = new System.Drawing.Size(64, 29);
			this.btnModification.TabIndex = 12;
			this.btnModification.Text = "修改";
			this.btnModification.Click += new System.EventHandler(this.btnModification_Click);
			// 
			// btndelete
			// 
			this.btndelete.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btndelete.Location = new System.Drawing.Point(160, 360);
			this.btndelete.Name = "btndelete";
			this.btndelete.Size = new System.Drawing.Size(64, 29);
			this.btndelete.TabIndex = 13;
			this.btndelete.Text = "删除";
			this.btndelete.Click += new System.EventHandler(this.btndelete_Click);
			// 
			// btnnext
			// 
			this.btnnext.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnnext.Location = new System.Drawing.Point(400, 360);
			this.btnnext.Name = "btnnext";
			this.btnnext.Size = new System.Drawing.Size(75, 30);
			this.btnnext.TabIndex = 19;
			this.btnnext.Text = "下一条";
			this.btnnext.Click += new System.EventHandler(this.btnnext_Click);
			// 
			// btnbefore
			// 
			this.btnbefore.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnbefore.Location = new System.Drawing.Point(320, 360);
			this.btnbefore.Name = "btnbefore";
			this.btnbefore.Size = new System.Drawing.Size(74, 29);
			this.btnbefore.TabIndex = 18;
			this.btnbefore.Text = "上一条";
			this.btnbefore.Click += new System.EventHandler(this.btnbefore_Click);
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button1.Location = new System.Drawing.Point(232, 360);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 29);
			this.button1.TabIndex = 15;
			this.button1.Text = "取消";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Test_info
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
			this.ClientSize = new System.Drawing.Size(498, 409);
			this.Controls.Add(this.btnnext);
			this.Controls.Add(this.btnbefore);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.btnIncrease);
			this.Controls.Add(this.btnModification);
			this.Controls.Add(this.btndelete);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Test_info";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "课程信息";
			this.Load += new System.EventHandler(this.Test_info_Load);
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Test_info_Load(object sender, System.EventArgs e)
		{
			//			thisAdapter.Fill(mydataset,"test");
			dataGrid1.SetDataBinding(mydataset,"test");
			txtID.DataBindings.Add("text",mydataset,"test.课程编号");
			txtName.DataBindings.Add("text",mydataset,"test.课程名称");
			bManager = this.BindingContext[mydataset,"test"];
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			
		}

		private void btnIncrease_Click(object sender, System.EventArgs e)
		{
			try
			{
				DataTable dt = mydataset.Tables["test"];			
				DataRow newRow = dt.NewRow();
				newRow["课程编号"] = txtID.Text.Trim();			
				newRow["课程名称"] = txtName.Text.Trim();									
				dt.Rows.Add(newRow);
				thisAdapter.Update(mydataset,"test");
				dt.AcceptChanges();
				MessageBox.Show("你的信息已经被成功的保存到数据库","恭喜");
				
			}
			catch(Exception e1)
			{
				//					MessageBox.Show("数据库'用户名'被占用,本窗体将关闭","出错");					
				MessageBox.Show(e1.Message);
				this.Close();				
				return;
			}
		}

		private void btndelete_Click(object sender, System.EventArgs e)
		{
			DialogResult result = MessageBox.Show("你是否真的要删除该项","注意",MessageBoxButtons.YesNo,MessageBoxIcon.Stop);
			if(result == DialogResult.Yes)
			{
				try
				{				
					DataTable dataTable = mydataset.Tables["test"];					
					int c = dataGrid1.CurrentRowIndex;
					dataTable.Rows[c].Delete();
					int nRows = thisAdapter.Update(mydataset,"test");
					mydataset.AcceptChanges();
					dataGrid1.SetDataBinding(mydataset,"test");	
				}
				catch(Exception e1)
				{
					MessageBox.Show(e1.Message);
				}
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnModification_Click(object sender, System.EventArgs e)
		{
			try
			{
				SqlConnection thisConnection=new SqlConnection("integrated security=SSPI;data source=.;persist security info=False;initial catalog=Score_mange");
				SqlDataAdapter da = new SqlDataAdapter("update test set Test_name='"+this.txtName.Text.Trim()+"' where Test_Id = '"+this.txtID.Text.Trim()+"'",thisConnection);
				DataSet ds = new DataSet();			
				da.Fill(ds,"test");
				MessageBox.Show("恭喜 ,修改成功");
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void btnbefore_Click(object sender, System.EventArgs e)
		{
			bManager.Position -=1;	
			
//			if(cmAmend.Position>0)
//			{
//				this.dataGrid1.UnSelect(cmAmend.Position);
//				cmAmend.Position--;
//				this.dataGrid1.Select(cmAmend.Position);
//				this.dataGrid1.CurrentRowIndex=cmAmend.Position;
//			}
//			return;
		}

		private void btnnext_Click(object sender, System.EventArgs e)
		{
			bManager.Position +=1;
		}
	}
}

⌨️ 快捷键说明

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