问题
this code inserts in the database
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();
}
then this is for 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);
}
am using the cell click event, such that when a cell is clicked, the contents of that row, that is the CCImage and CCSpn_CODE appear. The CCSpn_CODE appears in Ptxtspn_code textBox just fine. The problem is the byte[] image that i'm converting. it's displaying only the image of the first row. how can i make PpicBox display whatever image from whatever row i click on the datagridViewjust like 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);
}
回答1:
Note:
- In
CellClickevent you should check if click is not on row header or column header e.RowIndexis row index of clicked cell, ande.ColumnIndexis index of column of clicked cell- To get values of clicked row you can use:
yourDGV.Rows[e.RowIndex].Cells["GridColumnName"].valueyourDGV.Rows[e.RowIndex].Cells[2].Value((DataRowView)yourDGV.Rows[e.RowIndex].DataBoundItem)[2]((DataRowView)yourDGV.Rows[e.RowIndex].DataBoundItem)["DataTableColumnName"]
- I didn't have any idea about why you fill abaanaCC in
CellClickbecause you load data in form load event and so all values including CCImage is present id your data table. So I removed it. It seems that you should fill it only when you want to load data not here in cell click.
For example your code may like this:
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);
}
来源:https://stackoverflow.com/questions/32853725/how-to-create-master-detail-from-datagridview