📄 frmchengji.cs
字号:
}
}
for(int i=0;i<this.txt4.Text.Length;i++)
{
if(!Char.IsNumber(txt4.Text.ToString(),i))
{
MessageBox.Show("只能为数字");
txt4.Focus();
returnValue=false;
break;
}
}
for(int i=0;i<this.txt5.Text.Length;i++)
{
if(!Char.IsNumber(txt5.Text.ToString(),i))
{
MessageBox.Show("只能为数字");
txt5.Focus();
returnValue=false;
break;
}
}
for(int i=0;i<this.txt6.Text.Length;i++)
{
if(!Char.IsNumber(txt6.Text.ToString(),i))
{
MessageBox.Show("只能为数字");
txt6.Focus();
returnValue=false;
break;
}
}
}
return returnValue;
}
private void ShowDataGird() //显示DataGird的方法
{
objOleDbConn.Open();
objDataAdapter=new OleDbDataAdapter();
objDataSet=new DataSet();
objDataAdapter=new OleDbDataAdapter("select * from ChengJi",objOleDbConn);
objDataAdapter.Fill(objDataSet,"ChengJi");
this.dataGrid1.SetDataBinding(objDataSet,"ChengJi");
this.objOleDbConn.Close();
if(this.objDataSet.Tables["ChengJi"].Rows.Count==0)
{
this.btnDel.Enabled=false;
this.btnMu.Enabled=false;
}
else
{
this.btnDel.Enabled=true;
this.btnMu.Enabled=true;
}
}
private void ClearBox() //清空控件
{
this.txtName.Clear();this.txtNumber.Clear();this.txt1.Clear();this.txt2.Clear();this.txt3.Clear();this.txt4.Clear();
this.txt5.Clear();this.txt6.Clear();this.txt7.Clear();
}
private void button6_Click(object sender, System.EventArgs e) //显示上下文菜单
{
Point objp=new Point(380,245);
this.contextMenu1.Show(this,objp);
}
private void frmChengJi_Load(object sender, System.EventArgs e)
{
this.txt7.Enabled=false;
string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;";
strConnection+=@"Data Source=Student.mdb";
objOleDbConn=new OleDbConnection(strConnection);
objOleDbConn.Open();
objDataSet=new DataSet();
objDataAdapter=new OleDbDataAdapter("select * from ChengJi",objOleDbConn);
objDataAdapter.Fill(objDataSet,"ChengJi");
this.dataGrid1.SetDataBinding(objDataSet,"ChengJi");
this.objOleDbConn.Close();
user myUser = user.InitUser();
if(myUser.isPublicUser == false)
{
this.btnMu.Enabled=false;this.btnInt.Enabled=false;
this.btnDel.Enabled=false;this.btnTongJi.Enabled=false;
}
else
{
this.btnMu.Enabled=true;this.btnInt.Enabled=true;
this.btnDel.Enabled=true;this.btnTongJi.Enabled=true;
}
if(this.objDataSet.Tables["ChengJi"].Rows.Count==0)
{
this.btnDel.Enabled=false;
this.btnMu.Enabled=false;
}
}
private void btnInt_Click(object sender, System.EventArgs e) //插入按钮
{
if(this.txt7.Text.Trim()==string.Empty)
{
MessageBox.Show("请先统计平均分!");
}
else
{
if(ValidateFields())
{
string inCom="insert into ChengJi values ('"+this.txtName.Text+"','"+this.txtNumber.Text+"','"+this.txt1.Text+"','"+this.txt2.Text+"','"+this.txt3.Text+"','"+this.txt4.Text+"','"+this.txt5.Text+"','"+this.txt6.Text+"','"+this.txt7.Text+"')";
this.objOleDbCommand=new OleDbCommand(inCom,this.objOleDbConn);
try
{
this.objOleDbConn.Open();
this.objOleDbCommand.ExecuteNonQuery();
this.objOleDbConn.Close();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message);
this.objOleDbConn.Close();
}
this.dataGrid1.DataSource=null;
ShowDataGird();
ClearBox();
}
}
}
private void dataGrid1_Click(object sender, System.EventArgs e)
{
this.txtName.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,0];
this.txtNumber.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,1];
this.txt1.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,2];
this.txt2.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,3];
this.txt3.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,4];
this.txt4.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,5];
this.txt5.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,6];
this.txt6.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,7];
this.txt7.Text=(string)dataGrid1[dataGrid1.CurrentCell.RowNumber,8];
}
private void btnMu_Click(object sender, System.EventArgs e) //更新按钮
{
if(ValidateFields())
{
if(this.txt7.Text.Trim()==string.Empty)
{
MessageBox.Show("请先统计平均分!");
}
else
{
int inRow =dataGrid1.CurrentRowIndex; //获取当前选定行的索引
string updCmd ="update ChengJi set 姓名 ='"+this.txtName.Text+"',学号 ='"+this.txtNumber.Text+"',成绩1 ='"+this.txt1.Text+"',成绩2 ='"+this.txt2.Text+"',成绩3 ='"+this.txt3.Text+"',成绩4 ='"+this.txt4.Text+"',成绩5 ='"+this.txt5.Text+"',成绩6 ='"+this.txt6.Text+"',成绩7 ='"+this.txt7.Text+"' where 学号 = '"+this.dataGrid1[inRow,1].ToString()+"'";
this.objOleDbCommand=new OleDbCommand(updCmd,objOleDbConn);
try
{
objOleDbConn.Open();
objOleDbCommand.ExecuteNonQuery();
objOleDbConn.Close();
ShowDataGird();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message);
objOleDbConn.Close();
}
ClearBox();
}
}
}
private void btnDel_Click(object sender, System.EventArgs e) //删除按钮
{
string delCmd="Delete from ChengJi where 学号=@StuNum";
this.objDataAdapter.DeleteCommand=new OleDbCommand(delCmd,objOleDbConn);
objParam=objDataAdapter.DeleteCommand.Parameters.Add(new System.Data.OleDb.OleDbParameter("@StuNum",System.Data.OleDb.OleDbType.VarChar,50));
objParam.SourceColumn="学号";
objParam.SourceVersion=DataRowVersion.Original;
try
{
objDataSet.Tables["ChengJi"].Rows[dataGrid1.CurrentRowIndex].Delete();
objDataAdapter.Update(objDataSet,"ChengJi");
ShowDataGird();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
objOleDbConn.Close();
ClearBox();
}
}
private void btnTongJi_Click(object sender, System.EventArgs e) //统计按钮
{
if(ValidateFields())
{
string chengji1=this.txt1.Text.ToString();string chengji2=this.txt2.Text.ToString();string chengji3=this.txt3.Text.ToString();string chengji4=this.txt4.Text.ToString();string chengji5=this.txt5.Text.ToString();string chengji6=this.txt6.Text.ToString();
int chengji=(int.Parse(chengji1)+int.Parse(chengji2)+int.Parse(chengji3)+int.Parse(chengji4)+int.Parse(chengji5)+int.Parse(chengji6))/6;
this.txt7.Text=chengji.ToString();
}
}
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem2_Click(object sender, System.EventArgs e) //显示报表
{
frmChengJiReport objfrmChengJiReport=new frmChengJiReport();
objfrmChengJiReport.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
this.txtNumber.Clear();this.txtName.Clear();this.txt7.Clear();this.txt1.Clear();this.txt2.Clear();this.txt3.Clear();
this.txt4.Clear();this.txt5.Clear();this.txt6.Clear();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) //打印页面设计
{
Font objFont = new Font("Tahoma",11,FontStyle.Regular);
Brush objBrush = Brushes.Black;
Pen objPen=new Pen(objBrush);
objPen.Width=2;
int nLeft=this.pageSetupDialog1.PageSettings.Margins.Left;
int nTop=this.pageSetupDialog1.PageSettings.Margins.Top;
int nWidth=this.pageSetupDialog1.PageSettings.PaperSize.Width-(this.pageSetupDialog1.PageSettings.Margins.Left +this.pageSetupDialog1.PageSettings.Margins.Right );
int nHeight=this.pageSetupDialog1.PageSettings.PaperSize.Height -(this.pageSetupDialog1.PageSettings.Margins.Top +this.pageSetupDialog1.PageSettings.Margins.Bottom );
e.Graphics.DrawLine(objPen,nLeft,nTop,nLeft+nWidth,nTop);
e.Graphics.DrawLine(objPen,nLeft,nTop+nHeight,nLeft+nWidth,nTop+nHeight);
e.Graphics.DrawLine(objPen,nLeft,nTop,nLeft,nTop+nHeight);
e.Graphics.DrawLine(objPen,nLeft+nWidth,nTop,nLeft+nWidth,nTop+nHeight);
e.Graphics.DrawString("成绩总评",new Font("Garamond",20,FontStyle.Bold),objBrush,nLeft+200,nTop+10);
e.Graphics.DrawString("姓名",objFont,objBrush,nLeft+25,nTop+60);
e.Graphics.DrawString("学号",objFont,objBrush,nLeft+85,nTop+60);
e.Graphics.DrawString("数学",objFont,objBrush,nLeft+150,nTop+60);
e.Graphics.DrawString("专英",objFont,objBrush,nLeft+210,nTop+60);
e.Graphics.DrawString("大英",objFont,objBrush,nLeft+270,nTop+60);
e.Graphics.DrawString("邓论",objFont,objBrush,nLeft+330,nTop+60);
e.Graphics.DrawString("军事",objFont,objBrush,nLeft+400,nTop+60);
e.Graphics.DrawString("专业",objFont,objBrush,nLeft+470,nTop+60);
e.Graphics.DrawString("平均分",objFont,objBrush,nLeft+530,nTop+60);
try
{
string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;";
strConnection+=@"Data Source=Student.mdb";
objOleDbConn=new OleDbConnection(strConnection);
objOleDbConn.Open();
OleDbCommand objSqlCommand = new OleDbCommand("Select * from ChengJi",objOleDbConn);
OleDbDataReader objOleDbDataReader = objSqlCommand.ExecuteReader();
int nLine=1;
while(objOleDbDataReader.Read())
{
e.Graphics.DrawString(objOleDbDataReader["姓名"].ToString(),objFont,objBrush,nLeft+25,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["学号"].ToString(),objFont,objBrush,nLeft+93,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["成绩1"].ToString(),objFont,objBrush,nLeft+150,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["成绩2"].ToString(),objFont,objBrush,nLeft+220,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["成绩3"].ToString(),objFont,objBrush,nLeft+280,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["成绩4"].ToString(),objFont,objBrush,nLeft+340,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["成绩5"].ToString(),objFont,objBrush,nLeft+410,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["成绩6"].ToString(),objFont,objBrush,nLeft+480,nTop+60+nLine*30);
e.Graphics.DrawString(objOleDbDataReader["成绩7"].ToString(),objFont,objBrush,nLeft+540,nTop+60+nLine*30);
nLine++;
}
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message);
}
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.printPreviewDialog1.ShowDialog();
}
private void btnPrint_Click(object sender, System.EventArgs e)
{
if(this.printDialog1.ShowDialog()==DialogResult.OK )
{
this.printDocument1.Print();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -