c# - How to create Master Detail from datagridview -
this code inserts in 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 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 save" + ex); } condb.close(); }
then datagridview
private void update_load(object sender, eventargs e) { // todo: line of code loads data 'abaanadataset.abaanacc' table. can move, or remove it, needed. this.abaanacctableadapter.fill(this.abaanadataset.abaanacc); }
am using cell click event, such when cell clicked, contents of row, ccimage
, ccspn_code
appear. ccspn_code
appears in ptxtspn_code
textbox
fine. problem byte[] image i'm converting. it's displaying image of first row. how can make ppicbox
display whatever image whatever row click on datagridview
just 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); }
note:
- in
cellclick
event should check if click not on row header or column header e.rowindex
row index of clicked cell, ,e.columnindex
index of column of clicked cell- to values of clicked row can use:
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"]
- i didn't have idea why fill abaanacc in
cellclick
because load data in form load event , values including ccimage present id data table. removed it. seems should fill when want load data not here in cell click.
for example code may 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); }
Comments
Post a Comment