此代码插入数据库
private void btnSave_Click(object sender, EventArgs e)
{
byte[] imageBt = null;
FileStream fstream = new FileStream(this.txtImgPath.Text,FileMode.Open,FileAccess.Read);
BinaryReader Br = new BinaryReader(fstream);
imageBt = Br.ReadBytes((int)fstream.Length);
// byte[] pic = stream.ToArray();
try
{
conDB.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = conDB;
command.CommandText = "insert into abaanaCC (CCSpn_CODE,CCFname,CCLname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCImage)" +
" values ('" + spn_codetxt.Text + "','" + txtfname.Text + "','" + lnametxt.Text + "','" + mnametxt.Text + "','" + DOBDTPicker1.Text + "','" + gendercomboBox.Text + "','" + schtxt.Text + "','" + classcomboBox.Text + "','" + villatxt.Text + "','" + siblingscombobx.Text + "','" + guardiantxt.Text + "','" + contacttxt.Text + "',@IMG) ";
command.Parameters.Add(new OleDbParameter("@IMG",imageBt));
//command.Parameters.AddWithValue("@IMG",pic);
command.ExecuteNonQuery();
MessageBox.Show("Record Saved");
}
catch (Exception ex)
{
MessageBox.Show("Unable to save" + ex);
}
conDB.Close();
}
然后这是针对datagridview
private void Update_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'abaanaDataSet.abaanaCC' table. You can move, or remove it, as needed.
this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);
}
正在使用单元格点击事件,以便在单击某个单元格时,会显示该行的内容,即CCImage
和CCSpn_CODE
。 CCSpn_CODE
出现在Ptxtspn_code
textBox
就好了。问题是我正在转换的byte []图像。它只显示第一行的图像。我如何PpicBox
显示我点击datagridView
的任何行中的任何图片,就像Ptxtspn_code
textBox
private void abaanaCCDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.Ptxtspn_code.Text = this.abaanaCCDataGridView.SelectedRows[0].Cells[this.dataGridViewTextBoxColumn2.Name].Value.ToString();
this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);
byte[] mydata = (byte[])this.abaanaDataSet.abaanaCC.Rows[0]["CCImage"];
MemoryStream stream = new MemoryStream(mydata);
this.PpicBox.Image =Image.FromStream(stream);
}
答案 0 :(得分:0)
注意:
CellClick
事件中,您应该检查点击是否在行标题或列标题e.RowIndex
是已点击单元格的行索引,e.ColumnIndex
是已点击单元格列的索引yourDGV.Rows[e.RowIndex].Cells["GridColumnName"].value
yourDGV.Rows[e.RowIndex].Cells[2].Value
((DataRowView)yourDGV.Rows[e.RowIndex].DataBoundItem)[2]
((DataRowView)yourDGV.Rows[e.RowIndex].DataBoundItem)["DataTableColumnName"]
CellClick
中填写abaanaCC,因为你在表单加载事件中加载数据,所以包括CCImage在内的所有值都存在于你的数据表中。所以我删除了它。看来只有当你想在单元格点击中加载不在这里的数据时才应填写它。例如,您的代码可能是这样的:
private void abaanaCCDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
return;
this.Ptxtspn_code.Text = this.abaanaCCDataGridView.Rows[e.RowIndex].Cells[this.dataGridViewTextBoxColumn2.Name].Value.ToString();
byte[] mydata = (byte[])this.abaanaDataSet.abaanaCC.Rows[r.RowIndex]["CCImage"];
MemoryStream stream = new MemoryStream(mydata);
this.PpicBox.Image =Image.FromStream(stream);
}