📄 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_7
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(47, 15);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(53, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(47, 46);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(79, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// dataGrid1
//
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(6, 82);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.RowHeadersVisible = false;
this.dataGrid1.Size = new System.Drawing.Size(229, 121);
this.dataGrid1.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(146, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(55, 24);
this.button1.TabIndex = 3;
this.button1.Text = "上一页";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(146, 43);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(55, 24);
this.button2.TabIndex = 4;
this.button2.Text = "下一页";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(9, 19);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(34, 19);
this.label1.TabIndex = 5;
this.label1.Text = "学号";
//
// label2
//
this.label2.Location = new System.Drawing.Point(9, 51);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 29);
this.label2.TabIndex = 6;
this.label2.Text = "姓名";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(241, 211);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "学生成绩浏览";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
DataSet ds,ds1;
DataRelation studentScoreRel;
SqlDataAdapter sqlDataAdapter;
private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "server=localhost;uid=sa;pwd=;database=xsgl";
sqlDataAdapter=new SqlDataAdapter("SELECT studID,studName FROM student",myConnection);
ds=new DataSet();
sqlDataAdapter.Fill(ds,"student");
sqlDataAdapter=new SqlDataAdapter("SELECT * FROM score",myConnection);
sqlDataAdapter.Fill(ds,"score");
studentScoreRel = ds.Relations.Add("StudentScore",
ds.Tables["student"].Columns["studID"],
ds.Tables["score"].Columns["studID"]);
textBox1.DataBindings.Add(new Binding("Text",ds, "student.studID"));
textBox2.DataBindings.Add(new Binding("Text",ds, "student.studName"));
this.BindingContext[ds, "student"].PositionChanged +=new EventHandler(BindingManagerBase_PositionChanged);
ds1=new DataSet();
ds1.Tables.Add(ds.Tables["score"].Clone());
dataGrid1.DataSource=ds1;
dataGrid1.DataMember="score";
showScore();
}
private void BindingManagerBase_PositionChanged(object sender, EventArgs e)
{
showScore();
}
private void showScore()
{
ds1.Tables["score"].Rows.Clear();
DataRow dr=ds.Tables["student"].Rows[this.BindingContext[ds, "student"].Position];
foreach (DataRow dr1 in dr.GetChildRows(studentScoreRel))
{
DataRow myDR=ds1.Tables["score"].NewRow();
myDR["studID"]=dr1["studID"];
myDR["courseID"]=dr1["courseID"];
myDR["score"]=dr1["score"];
ds1.Tables["score"].Rows.Add(myDR);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
this.BindingContext[ds, "student"].Position-=1;
}
private void button2_Click(object sender, System.EventArgs e)
{
this.BindingContext[ds, "student"].Position+=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -