学习笔记之C#各类控件数据的输入与输出
学习笔记之C#各类控件数据的输入与输出
一、知识点描述
1、DateTimePicker控件使用过程中要注意手动进行数据的强制转换,示例如下:
dtp_BirthDate.Value= (DateTime)dataGridView1.CurrentRow.Cells[3].Value;
2、PhotoBox的使用比较复杂,使用之前需要声明并实例化打开文件对话框进行图片选择,需要设置好对话框的各种属性。输出图片时,要判断存放图片的数组是否为数据库空值,如果是,需要将其转换为C#里面的null。特别注意的是,需要定义一个数组去存放照片。除此之外,还应考虑图片清空的问题。
3、ComBobox通过一些属性的设置可以实现自动查询功能:
设置如下:
cmb_SearchId.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmb_SearchId.AutoCompleteSource= AutoCompleteSource.ListItems;
再将ComboBox与数据库相应列的值进行绑定,可以大大简化查询过程。
二、思维导图:
三、效果载图
1、DateTimePicker
输入:
输出:
2、PhotoBox
输入:
输出:
3、RedioButton
输出:
输入:
4、ComBobox
输出:
输入:
5、DataDridView
输出:
6、TextBox
输出:
输入:
四、示例代码
² ComBobox、RedioButton、DateTimePicker、PhotoBox、TextBox的数据输出
private void txb_PYM_MouseEnter(object sender, EventArgs e)
{
this.tot_PYM .ToolTipTitle = "请注意";
this.tot_PYM .IsBalloon = false;
this.tot_PYM .UseFading = true;
this.tot_PYM .Show("拼音码为每个汉字的首字母,如张三:ZS", this.txb_PYM);
}
private void txb_PYM_MouseLeave(object sender, EventArgs e)
{
this.tot_PYM.Hide(txb_PYM );
}
private void btn_Add_Click(object sender, EventArgs e)
{
MemoryStream memoryStream = new MemoryStream();
this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Bmp);
byte[] photoBytes = newbyte[memoryStream.Length];
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.Read(photoBytes, 0, photoBytes.Length);
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText =
"INSERT tb_patient (P_id, P_name,P_sex,P_birthday, P_nation,Feetype,P_tel,P_pym,P_address,P_career,P_bloodtype,P_allergic,P_idcard,P_Photo) VALUES(@P_id,@P_name,@P_sex,@P_birthday,@P_nation,@Feetype,@P_tel,@P_pym,@P_address,@P_career,@P_bloodtype,@P_allergic,@P_idcard,@P_Photo);";
sqlCommand.Parameters.AddWithValue("@P_id", this.txb_Id.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_name", this.txb_PatientName.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_sex", rdb_SexBoy.Checked ? "男" : "女");
sqlCommand.Parameters.AddWithValue("@P_birthday", this.dtp_BirthDate.Value);
sqlCommand.Parameters.AddWithValue("@P_nation", this.cmb_Nation.Text.Trim());
sqlCommand.Parameters.AddWithValue("@Feetype", this.com_FeeType.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_tel", this.txb_Tel.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_pym", this.txb_PYM.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_address", this.txb_Address.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_career", this.cmb_Career.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_bloodtype", this.cmb_Blood.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_allergic", this.txb_Allergic.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_idcard", this.txb_IdCard.Text.Trim());
sqlCommand.Parameters.AddWithValue("@P_Photo", photoBytes);
sqlConnection.Open();
int rowAffected = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
if (rowAffected == 1)
{
MessageBox.Show("添加成功!");
frm_PatientInformationManage PatientInformationManageForm = new frm_PatientInformationManage();
PatientInformationManageForm.ShowDialog(this);
}
else
{
MessageBox.Show("添加失败!");
}
}
private void btn_OpenPhoto_Click(object sender, EventArgs e)
{
OpenFileDialog openPhotoDialog = new OpenFileDialog()
Title = "打开照片文件(位图格式)"
,
Filter = "BMP Files (*.bmp)|*.bmp"
,
InitialDirectory = @"C:\"
};
if (openPhotoDialog.ShowDialog() == DialogResult.OK)
{
this.PhotoFileName = openPhotoDialog.FileName;
this.ptb_Photo.Image = Image.FromFile(this.PhotoFileName);
}
}
² ComBobox、RedioButton、DateTimePicker、PhotoBox、TextBox的数据输入
private void btn_SearchId_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText =
" select * from tb_patient where P_id=@P_id";
sqlCommand.Parameters.AddWithValue("@P_id", this.cmb_SearchId .Text.Trim());
sqlConnection.Open();
bool dd = true;
try
{
sqlCommand.ExecuteScalar();
}
catch
{
dd = false;
}
finally
{
sqlConnection.Close();
}
if (dd)
{
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand);
sda.Fill(ds, "tb_patient");
DataTable dt = ds.Tables["tb_patient"];
txb_Id.Text = dt.Rows[0]["P_id"].ToString();
txb_PatientName.Text = dt.Rows[0]["P_name"].ToString();
if (dt.Rows[0]["P_sex"].ToString() == "男")
{
rdb_SexBoy.Checked = true;
}
else
{
rdb_SexGirl.Checked = true;
}
dtp_BirthDate.Value = (DateTime)dt.Rows[0]["P_birthday"];
cmb_Nation.Text = dt.Rows[0]["P_nation"].ToString();
cmb_FeeType.Text = dt.Rows[0]["Feetype"].ToString();
txb_Tel.Text = dt.Rows[0]["P_tel"].ToString();
txb_PYM.Text = dt.Rows[0]["P_pym"].ToString();
txb_Address.Text = dt.Rows[0]["P_address"].ToString();
cmb_Career .Text = dt.Rows[0]["P_career"].ToString();
cmb_Blood.Text = dt.Rows[0]["P_bloodtype"].ToString();
txb_Allergic.Text = dt.Rows[0]["P_allergic"].ToString();
txb_IdCard.Text = dt.Rows[0]["P_idcard"].ToString();
byte[] photoBytes = null;
photoBytes =
(dt.Rows[0]["P_Photo"] == DBNull.Value ? null : (byte[])dt.Rows[0]["P_Photo"]);
if (photoBytes == null)
{
ptb_Photo.Image = null;
}
if (photoBytes != null)
{
MemoryStream memoryStream = new MemoryStream(photoBytes);
this.ptb_Photo.Image = Image.FromStream(memoryStream);
}
}
}
² DataGridView的输入
private void frm_PatientInformationManage_Load(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "select * from tb_patient";
SqlCommand sqlCommand1 = new SqlCommand();
sqlCommand1.Connection = sqlConnection;
sqlCommand1.CommandText = "select * from tb_user where U_type='3';";
sqlConnection.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlCommand;
DataSet ds = new DataSet();
da.Fill(ds, "tb_patient");
dataGridView1.DataSource = ds.Tables["tb_patient"];
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataTable tb_patient = new DataTable(); sqlDataAdapter.Fill(tb_patient);
this.cmb_SearchPYM .DataSource = tb_patient;
this.cmb_SearchPYM.DisplayMember = "P_pym";
this.cmb_SearchId .DataSource = tb_patient;
this.cmb_SearchId.DisplayMember = "P_id";
cmb_SearchId.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmb_SearchId.AutoCompleteSource = AutoCompleteSource.ListItems;
cmb_SearchPYM.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmb_SearchPYM.AutoCompleteSource = AutoCompleteSource.ListItems;
}