📄 addcourse.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace StudentManagementsystem
{
/// <summary>
/// AddCourse 的摘要说明。
/// </summary>
public class AddCourse : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid CourList;
private System.Windows.Forms.Button AddCour;
private System.Windows.Forms.Button ChgCour;
private System.Windows.Forms.Button DelCour;
private System.Windows.Forms.Button ExitCour;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private string AddCourConnStr="workstation id=localhost;Integrated Security=SSPI;database=StuMagSys";
private SqlConnection AddCourConn;
private string AddCourSql;
private SqlCommandBuilder AddCourComm;
private SqlDataAdapter AddCourDA;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private DataSet AddCourseset=new DataSet();
public AddCourse()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.AddCourse_Load();
//
// 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.CourList = new System.Windows.Forms.DataGrid();
this.AddCour = new System.Windows.Forms.Button();
this.ChgCour = new System.Windows.Forms.Button();
this.DelCour = new System.Windows.Forms.Button();
this.ExitCour = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.CourList)).BeginInit();
this.SuspendLayout();
//
// CourList
//
this.CourList.DataMember = "";
this.CourList.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.CourList.Location = new System.Drawing.Point(32, 24);
this.CourList.Name = "CourList";
this.CourList.Size = new System.Drawing.Size(368, 200);
this.CourList.TabIndex = 0;
//
// AddCour
//
this.AddCour.Location = new System.Drawing.Point(56, 256);
this.AddCour.Name = "AddCour";
this.AddCour.Size = new System.Drawing.Size(72, 24);
this.AddCour.TabIndex = 1;
this.AddCour.Text = "增加课程";
this.AddCour.Click += new System.EventHandler(this.AddCour_Click);
//
// ChgCour
//
this.ChgCour.Location = new System.Drawing.Point(216, 256);
this.ChgCour.Name = "ChgCour";
this.ChgCour.Size = new System.Drawing.Size(72, 24);
this.ChgCour.TabIndex = 2;
this.ChgCour.Text = "保存修改";
this.ChgCour.Click += new System.EventHandler(this.ChgCour_Click);
//
// DelCour
//
this.DelCour.Location = new System.Drawing.Point(136, 256);
this.DelCour.Name = "DelCour";
this.DelCour.Size = new System.Drawing.Size(72, 24);
this.DelCour.TabIndex = 3;
this.DelCour.Text = "删除课程";
this.DelCour.Click += new System.EventHandler(this.DelCour_Click);
//
// ExitCour
//
this.ExitCour.Location = new System.Drawing.Point(296, 256);
this.ExitCour.Name = "ExitCour";
this.ExitCour.Size = new System.Drawing.Size(72, 24);
this.ExitCour.TabIndex = 4;
this.ExitCour.Text = "退出";
this.ExitCour.Click += new System.EventHandler(this.ExitCour_Click);
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(24, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(384, 232);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "现有课程信息";
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(24, 240);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(384, 48);
this.groupBox2.TabIndex = 6;
this.groupBox2.TabStop = false;
//
// AddCourse
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 309);
this.Controls.Add(this.ExitCour);
this.Controls.Add(this.DelCour);
this.Controls.Add(this.ChgCour);
this.Controls.Add(this.AddCour);
this.Controls.Add(this.CourList);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Name = "AddCourse";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "课程管理";
((System.ComponentModel.ISupportInitialize)(this.CourList)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void AddCourse_Load()
{
AddCourSql="select Course_name as 课程名,credit as 学分 from course";
this.AddCourConn=new SqlConnection(this.AddCourConnStr);
this.AddCourDA=new SqlDataAdapter(this.AddCourSql,this.AddCourConn);
AddCourseset.Clear();
this.AddCourDA.Fill(AddCourseset,"Course");
CourList.DataSource=AddCourseset.Tables[0];
}
private void ChgCour_Click(object sender, System.EventArgs e)
{
try
{
int row=this.CourList.CurrentCell.RowNumber;
this.CourList.CurrentCell=new DataGridCell(row+1,0);
if(AddCourseset.HasChanges())
{
this.AddCourConn=new SqlConnection(this.AddCourConnStr);
this.AddCourDA=new SqlDataAdapter(this.AddCourSql,this.AddCourConn);
AddCourComm=new SqlCommandBuilder(AddCourDA);
this.AddCourDA.Update(this.AddCourseset.GetChanges(),"Course");
MessageBox.Show("保存修改成功!","信息");
}
}
catch(Exception Ee)
{
MessageBox.Show(Ee.ToString(),"信息");
}
}
private void ExitCour_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("您确认要退出本系统?","确认",MessageBoxButtons.YesNoCancel)==DialogResult.Yes)
this.Close();
}
private void DelCour_Click(object sender, System.EventArgs e)
{
int rowNumber=this.CourList.CurrentCell.RowNumber;
try
{
this.AddCourseset.Tables[0].Rows[rowNumber].Delete();
this.AddCourConn=new SqlConnection(this.AddCourConnStr);
AddCourDA=new SqlDataAdapter("",AddCourConn);
AddCourComm=new SqlCommandBuilder(AddCourDA);
AddCourDA.Update(this.AddCourseset.GetChanges(),"Course");
MessageBox.Show("删除成功!","信息");
}
catch
{}
}
private void AddCour_Click(object sender, System.EventArgs e)
{
int row=this.CourList.VisibleRowCount;
this.CourList.CurrentCell=new DataGridCell(row+1,0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -