📄 form1.cs
字号:
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(464, 232);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(96, 21);
this.textBox1.TabIndex = 18;
this.textBox1.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(392, 232);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 19;
this.label2.Text = "当前记录:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 365);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.buttonPrint);
this.Controls.Add(this.buttonFromPhoto);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.buttonAppend);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonDelete);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.buttonClearPhoto);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "个人情况表";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public void BuildAdapter(string selectString)
{
SqlConnection conn=new SqlConnection(connString);
adapter=new SqlDataAdapter(selectString,conn);
SqlCommandBuilder builder=new SqlCommandBuilder(adapter);
adapter.DeleteCommand=builder.GetDeleteCommand();
adapter.InsertCommand=builder.GetInsertCommand();
adapter.UpdateCommand=builder.GetUpdateCommand();
}
private void FillComboBox(ref ComboBox combobox,string strTable)
{
string sqlstr="select * from "+strTable;
SqlConnection conn=new SqlConnection(connString);
conn.Open();
try
{
SqlCommand command=new SqlCommand(sqlstr,conn);
SqlDataReader r=command.ExecuteReader();
while(r.Read())
{
combobox.Items.Add("["+r["编码"]+"]"+r["名称"]);
}
r.Close();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
if(conn.State==ConnectionState.Open)
{
conn.Close();
}
}
}
private void buttonAppend_Click(object sender, System.EventArgs e)
{
string bh=this.comboBox1.SelectedItem.ToString().Substring(1,2)+"000";
int num=Int32.Parse(bh)+1;
DataRow newRow=dataset.Tables[0].NewRow();
while(true)
{
try
{
newRow["编号"]=num.ToString("d5");
newRow["性别"]="男";
dataset.Tables[0].Rows.Add(newRow);
break;
}
catch
{
num++;
continue;
}
}
}
private void buttonDelete_Click(object sender, System.EventArgs e)
{
int num=dataview.Count-1;
if(num>-1)
{
ArrayList ar=new ArrayList();
for(int i=num;i>=0;i--)
{
if(this.dataGrid1.IsSelected(i))
{
ar.Add(i);
}
}
if(ar.Count>0)
{
if(MessageBox.Show("确实要删除选中的"+ar.Count.ToString()+
"行吗? 注意:删除后就无法恢复了!","警告",
MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
foreach(int i in ar)
{
dataview.Delete(i);
}
adapter.Update(dataset,tableName);
}
}
else
{
MessageBox.Show("请先用鼠标点击左边灰色部分选择要删除的"+
"行(可以按住Ctrl键或者Shift键同时用鼠标选中多行),然后再删除。","提示");
}
}
}
private void buttonSave_Click(object sender, System.EventArgs e)
{
try
{
adapter.Update(dataset,tableName);
MessageBox.Show("保存成功。","恭喜");
}
catch(Exception err)
{
MessageBox.Show(err.Message,"保存信息出错!",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
private void comboBox1_SelectionChangeCommitted(object sender, System.EventArgs e)
{
if(dataset.HasChanges())
{
try
{
adapter.Update(dataset,tableName);
}
catch(Exception err)
{
MessageBox.Show(err.Message+"\n\n请先修改错误!","保存信息出错!",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
}
string str="-1";
if(this.comboBox1.SelectedIndex>=0)
{
str=this.comboBox1.SelectedItem.ToString().Substring(1,2);
dataview.RowFilter="substring(编号,1,2)='"+str+"'";
}
}
private void buttonCancel_Click(object sender, System.EventArgs e)
{
if(dataset.HasChanges())
{
if(MessageBox.Show("数据尚未保存,确实放弃未保存的修改吗?",
"小心",MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Close();
}
}
else
{
this.Close();
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(dataset.HasChanges())
{
if(MessageBox.Show("数据尚未保存,退出会引起修改无效!确实要退出吗?","小心",
MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
e.Cancel=false; //关闭窗体
}
else
{
e.Cancel=true; //不关闭窗体
}
}
}
private void buttonFromPhoto_Click(object sender, System.EventArgs e)
{
string bianhao=this.textBox1.Text;
if(bianhao.Length<5)
{
MessageBox.Show("请先选择记录(点击每行左边的灰色部分)。");
return;
}
bianhao=bianhao.Substring(0,5);
OpenFileDialog myfile=new OpenFileDialog();
if(myfile.ShowDialog() == DialogResult.OK)
{
try
{
//根据打开的图像文件创建原始图像大小的Bitmap对象
Bitmap bitmap=new Bitmap(myfile.OpenFile());
//缩放到112像素宽,144像素高
Bitmap image=new Bitmap(bitmap,112,144);
//创建内存流
MemoryStream memStream=new MemoryStream();
//将图像以jpeg格式保存到内存流中
image.Save(memStream,System.Drawing.Imaging.ImageFormat.Jpeg);
//将内存流数据写入字节数组
byte[] bytes=memStream.ToArray();
//关闭内存流
memStream.Close();
//求当前行号
int num=this.dataGrid1.CurrentRowIndex;
//保存字节数组到当前行的照片字段中
dataset.Tables[0].Rows[num]["照片"]=bytes;
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
GetImage(this.textBox1.Text.Substring(0,5));
}
}
private void dataGrid1_Click(object sender, System.EventArgs e)
{
int num=this.dataGrid1.CurrentRowIndex;
if(num>-1)
{
this.textBox1.Text=dataset.Tables[0].Rows[num]["编号"].ToString()+
" "+dataset.Tables[0].Rows[num]["姓名"].ToString();
GetImage(this.textBox1.Text.Substring(0,5));
}
}
private void GetImage(string str)
{
int num=this.dataGrid1.CurrentRowIndex;
if(num==-1)
{
return;
}
if(dataset.Tables[0].Rows[num]["照片"]!=Convert.DBNull)
{
byte[] bytes=(byte[])dataset.Tables[0].Rows[num]["照片"];
MemoryStream memStream=new MemoryStream(bytes);
Bitmap bitmap = new Bitmap(memStream);
memStream.Close();
this.pictureBox1.Image = bitmap;
}
else
{
this.pictureBox1.Image = null;
}
}
private void buttonClearPhoto_Click(object sender, System.EventArgs e)
{
int num=this.dataGrid1.CurrentRowIndex;
if(num>-1)
{
dataset.Tables[0].Rows[num]["照片"]=Convert.DBNull;
GetImage(this.textBox1.Text.Substring(0,5));
}
}
private void buttonPrint_Click(object sender, System.EventArgs e)
{
Form prt=new print();
prt.ShowDialog();
}
//改变当前单元格位置时发生
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
dataGrid1_Click(null,null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -