📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Exam6_5
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// 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.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// button5
//
this.button5.Location = new System.Drawing.Point(30, 167);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(52, 26);
this.button5.TabIndex = 21;
this.button5.Text = "添加";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(101, 167);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(52, 26);
this.button6.TabIndex = 22;
this.button6.Text = "修改";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(172, 167);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(52, 26);
this.button7.TabIndex = 23;
this.button7.Text = "保存";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(243, 167);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(52, 26);
this.button8.TabIndex = 24;
this.button8.Text = "取消";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// dataGrid1
//
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(6, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(280, 151);
this.dataGrid1.TabIndex = 29;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox3);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(295, 11);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(189, 103);
this.groupBox1.TabIndex = 30;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "成绩记录";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(67, 71);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(59, 21);
this.textBox3.TabIndex = 5;
this.textBox3.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(19, 76);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(45, 19);
this.label3.TabIndex = 4;
this.label3.Text = "成绩";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(67, 42);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(53, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(19, 49);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(46, 16);
this.label2.TabIndex = 2;
this.label2.Text = "课程号";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(67, 14);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(54, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(19, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 21);
this.label1.TabIndex = 0;
this.label1.Text = "学号";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button4);
this.groupBox2.Controls.Add(this.button3);
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Location = new System.Drawing.Point(295, 117);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(189, 41);
this.groupBox2.TabIndex = 31;
this.groupBox2.TabStop = false;
//
// button4
//
this.button4.Location = new System.Drawing.Point(141, 11);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(40, 24);
this.button4.TabIndex = 32;
this.button4.Text = "尾页";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(95, 11);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(40, 24);
this.button3.TabIndex = 31;
this.button3.Text = "首页";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(49, 11);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(40, 24);
this.button2.TabIndex = 30;
this.button2.Text = "下页";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(6, 11);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 24);
this.button1.TabIndex = 29;
this.button1.Text = "上页";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(314, 167);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(52, 26);
this.button9.TabIndex = 32;
this.button9.Text = "删除";
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(385, 167);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(52, 26);
this.button10.TabIndex = 33;
this.button10.Text = "退出";
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(490, 199);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Name = "Form1";
this.Text = "学生成绩维护";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
SqlDataAdapter sqlDataAdapter;
DataSet dsScore;
int cur;
private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "server=localhost;uid=sa;pwd=;database=xsgl";
SqlCommand sqlCommand=new SqlCommand();
sqlCommand.Connection=myConnection;
sqlCommand.CommandType=CommandType.Text;
sqlCommand.CommandText ="SELECT * FROM score";
sqlDataAdapter=new SqlDataAdapter();
sqlDataAdapter.SelectCommand=sqlCommand;
SqlCommandBuilder builder=new SqlCommandBuilder(sqlDataAdapter);
dsScore=new DataSet();
sqlDataAdapter.Fill(dsScore,"score");
this.BindingContext[dsScore, "score"].PositionChanged +=new EventHandler(BindingManagerBase_PositionChanged);
textBox1.DataBindings.Add(new Binding("Text",dsScore, "score.studID"));//Text为要绑定的控件属性,mydv为数据视图,productID为字段名称。
textBox2.DataBindings.Add(new Binding("Text",dsScore, "score.courseID"));//Text为要绑定的控件属性,mydv为数据视图,productID为字段名称。
textBox3.DataBindings.Add(new Binding("Text",dsScore, "score.score"));//Text为要绑定的控件属性,mydv为数据视图,productID为字段名称。
dataGrid1.DataSource=dsScore;
dataGrid1.DataMember="score";
ShowPosition();
setState(true);
}
private void button1_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsScore, "score"].Position-=1;
}
private void button2_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsScore, "score"].Position+=1;
}
private void button3_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsScore, "score"].Position=0;
}
private void button4_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsScore, "score"].Position=this.BindingContext[dsScore, "score"].Count-1;
}
private void ShowPosition()
{
int iCnt;
int iPos;
iCnt = this.BindingContext[dsScore, "score"].Count;
iPos = this.BindingContext[dsScore, "score"].Position + 1;
if(iCnt == 0)
{
groupBox1.Text = "无记录";
}
else
{
groupBox1.Text = iPos.ToString() + " of " + iCnt.ToString() ;
}
}
private void BindingManagerBase_PositionChanged
(object sender, EventArgs e)
{
// 显示当前位置
ShowPosition();
}
private void button5_Click(object sender, System.EventArgs e)
{
cur=this.BindingContext[dsScore, "score"].Position;
textBox1.Focus();
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
setState(false);
}
private void button7_Click(object sender, System.EventArgs e)
{
try
{
DataRow dr=dsScore.Tables["score"].NewRow();
dr["studID"]=Int32.Parse(textBox1.Text);
dr["courseID"]=Int32.Parse(textBox2.Text);
dr["score"]=Single.Parse(textBox3.Text);
dsScore.Tables["score"].Rows.Add(dr);
sqlDataAdapter.Update(dsScore,"score");
this.BindingContext[dsScore, "score"].Position=this.BindingContext[dsScore, "score"].Count-1;
}
catch(Exception ee){MessageBox.Show("保存数据失败:"+ee.Message);}
}
private void button8_Click(object sender, System.EventArgs e)
{
dsScore.Clear();
sqlDataAdapter.Fill(dsScore,"score");
setState(true);
this.BindingContext[dsScore, "score"].Position=cur;
}
private void button6_Click(object sender, System.EventArgs e)
{
cur=this.BindingContext[dsScore, "score"].Position;
setState(false);
textBox1.ReadOnly=true;
textBox2.Focus();
}
private void button9_Click(object sender, System.EventArgs e)
{
dsScore.Tables["score"].Rows[this.BindingContext[dsScore, "score"].Position].Delete();
sqlDataAdapter.Update(dsScore,"score");
}
private void setState(bool b)
{
button1.Enabled=b;
button2.Enabled=b;
button3.Enabled=b;
button4.Enabled=b;
button5.Enabled=b;
button6.Enabled=b;
button7.Enabled=!b;
button8.Enabled=!b;
button9.Enabled=b;
button10.Enabled=b;
dataGrid1.Enabled=b;
textBox1.ReadOnly=b;
textBox2.ReadOnly=b;
textBox3.ReadOnly=b;
}
private void button10_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -