📄 classstu.cs
字号:
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(64, 23);
this.label8.TabIndex = 13;
this.label8.Text = "手机";
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// checkBox1
//
this.checkBox1.Enabled = false;
this.checkBox1.Location = new System.Drawing.Point(501, 373);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 17;
this.checkBox1.Text = "毕业否";
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(507, 424);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 18;
this.btnCancel.Text = "取消";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(389, 424);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 19;
this.btnSave.Text = "保存";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(35, 424);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 20;
this.btnAdd.Text = "添加记录";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(271, 424);
this.btnRemove.Name = "btnRemove";
this.btnRemove.TabIndex = 21;
this.btnRemove.Text = "删除记录";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// btnChange
//
this.btnChange.Location = new System.Drawing.Point(153, 424);
this.btnChange.Name = "btnChange";
this.btnChange.TabIndex = 22;
this.btnChange.Text = "修改记录";
this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
//
// ClassStu
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(616, 466);
this.Controls.Add(this.btnChange);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.txtPhone);
this.Controls.Add(this.txtCellPhone);
this.Controls.Add(this.txtDate);
this.Controls.Add(this.txtClass);
this.Controls.Add(this.txtAddress);
this.Controls.Add(this.txtID);
this.Controls.Add(this.txtName);
this.Controls.Add(this.txtSex);
this.Controls.Add(this.label7);
this.Controls.Add(this.label8);
this.Controls.Add(this.label4);
this.Controls.Add(this.label5);
this.Controls.Add(this.label6);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.dataGrid1);
this.Name = "ClassStu";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "学生情况数据维护";
this.Load += new System.EventHandler(this.ClassStu_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void ClassStu_Load(object sender, System.EventArgs e)
{
this.sendStrSQL="select * from STU where(班级编号='"+ClassID.UserID+"')";
this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="STU";
this.dataGrid1.CaptionText=ClassID.result+"学生情况";
cmOrders=(CurrencyManager)BindingContext[ds,"STU"];
//代码绑定文本框
this.txtID.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.学号"));
this.txtName.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.姓名"));
this.txtSex.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.性别"));
this.txtDate.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.出生"));
this.txtClass.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.班级编号"));
this.txtAddress.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.地址"));
this.txtPhone.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.电话"));
this.txtCellPhone.DataBindings.Add(new System.Windows.Forms.Binding("Text",ds,"STU.手机"));
this.checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Checked", ds, "STU.毕业否"));
/*if(ds.Tables["STU"].Rows[cmOrders.Position][8].ToString()=="False")
{
checkBox1.Checked=false;
}
else
{
checkBox1.Checked=true;
}*/
}
private void SetModifyMode(bool blnEdit)
{
this.txtID.ReadOnly=!blnEdit;
txtName.ReadOnly=!blnEdit;
txtSex.ReadOnly=!blnEdit;
txtDate.ReadOnly=!blnEdit;
txtClass.ReadOnly=!blnEdit;
txtAddress.ReadOnly=!blnEdit;
txtPhone.ReadOnly=!blnEdit;
txtCellPhone.ReadOnly=!blnEdit;
checkBox1.Enabled=blnEdit;
}
//获取最大学号
private void SetDefaultValue()
{
string StrConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(StrConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandText="select max(学号) 最大学号 from STU";
int maxID=0;
object result=cmd.ExecuteScalar();
if(result!=System.DBNull.Value)
{
maxID=Convert.ToInt32(result)+1;
}
txtID.Text=maxID.ToString();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
cmOrders.AddNew();
SetModifyMode(true);
SetDefaultValue();
txtName.Text="";
txtSex.Text="";
txtDate.Text="";
txtAddress.Text="";
txtPhone.Text="";
txtCellPhone.Text="";
}
private void btnChange_Click(object sender, System.EventArgs e)
{
SetModifyMode(true);
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
/*if(MessageBox.Show("确实要删除这条记录?","询问",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
try
{
cmOrders.EndCurrentEdit();
string strConn="workstation id=localhost;Integrated Security=SSPI;database=stu01";
SqlConnection conn=new SqlConnection(strConn);
conn.Open();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandText="delete from STU where 学号='"+this.txtID.Text+"'";
cmd.ExecuteNonQuery();
conn.Close();
ds.STU.Clear();
da1.Fill(ds);
}
catch(Exception express)
{
MessageBox.Show(express.ToString(),"信息",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
return;
}
}*/
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if(ds.GetChanges()!=null)
{
if(maxID!=0)
{
try
{
this.sendStrSQL="insert STU([学号],[姓名],[性别],[出生],[班级编号],[地址],[电话],[手机],[毕业否])"
+" values('"+txtID.Text.Trim()+"','"+txtName.Text.Trim()+"','"+txtSex.Text.Trim()+"','"+txtClass.Text.Trim()+"','"+txtAddress.Text.Trim()+"','"+txtPhone.Text.Trim()
+"','"+txtCellPhone.Text.Trim()+"','"+checkBox1.Text.Trim()+"')";
this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
link.UpdateDataBase(ds,"STU");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="STU";
SetModifyMode(false);
}
catch(Exception express)
{
MessageBox.Show(express.ToString(),"信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
}
else
{
try
{
this.sendStrSQL="update STU set 学号='"+txtID.Text.Trim()+"',姓名='"+txtName.Text.Trim()
+"',性别='"+txtSex.Text.Trim()+"',出生='"+txtDate.Text.Trim()+"',班级编号='"+txtClass.Text.Trim()
+"',地址='"+txtAddress.Text.Trim()+"',电话='"+txtPhone.Text.Trim()+"',手机='"+txtCellPhone.Text.Trim()+"',毕业否='"+checkBox1.Text.Trim()+"'";
this.ds=link.SelectDataBase(sendStrSQL,sendTableName);
link.UpdateDataBase(ds,"STU");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="STU";
SetModifyMode(false);
}
catch(Exception express)
{
MessageBox.Show(express.ToString(),"信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
}
/*this.dataGrid1.DataSource=dataSet11;
this.dataGrid1.DataMember="STU";*/
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
SetModifyMode(false);
cmOrders.CancelCurrentEdit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -