c# - Load data to a datagrid using another form -
i have 2 forms. 1st 1 frmstudentdetails. has datagrid 2nd 1 frmstudentregistration. has text boxes , add button
when user enter information , press "add" button, want add datagrid 1 one
for accomplish 1st created following method in frmstudentdetails
public void addrecord(string stid, string name) { datagridviewrow row = (datagridviewrow)dgvstdetails.rows[0].clone(); row.cells[0].value = stid; row.cells[1].value = name; dgvstdetails.rows.add(row); }
i called on frmstudentregistration form's add button -->
private void btnadd_click(object sender, eventargs e) { frmstudentdetailsform frm = new frmstudentdetailsform(); frm.addrecord(txtstudentid.text, txtstname.text); frm.showdialog(); }
then problem is, itz generating new forms show every new record. want add records in 1 form.
please me that
in "frmstudentregistration" class, add "public frmstudentdetailsform studentdetailsform { get; set; };" property declaration @ class level.
set equal instance of "frmstudentdetailsform" class. there several ways can (i.e. in "frmstudentregistration" class's custom constructor or "load" event handler), novice, recommend setting after instantiate "frmstudentregistration" class , before call instance variable's "show" method. note: if call "frmstudentregistration"'s "showdialog" method, updates other forms (i.e. "frmstudentdetailsform") won't show on screen until exit "showdialog" or explicity call other form's "show" or "showdialog" methods.
on side note, i'm curious why you're calling "frmstudentdetailsform"'s "showdialog" method "frmstudentregistration". in experience, either: a) grid's form call "showdialog" on add-item's form "add" option (i.e. via button, context menu item, insert key and/or enter key (after filling in new template row)) being on grid's form or b) both forms remain open @ same time (via modeless "show" method calls) "add" option on add-item's form. btw, in experience "frmstudentregistration" form called "frmstudentdetailsform" , "frmstudentdetailsform" called "frmstudentsform", "frmstudentslistform" or "frmstudentsummariesform", etc. also, btw, .net naming convention "var studentdetailsform = new studentdetailsform()" (i.e. suffix vs. prefix/both , no abbrev.). actually, if me, "var studentdetailsfrmobj = new studentdetailsfrmcls()". ;)
ex.
in "frmstudentregistration": public frmstudentdetailsform studentdetailsform { get; set; } public void main () { var studentregistration = new frmstudentregistration(); var studentdetailsform = new frmstudentdetailsform(); studentregistration.studentdetailsform = studentdetailsform; studentregistration.show(); } in "frmstudentregistration": private void btnadd_click(object sender, eventargs e) { studentdetailsform.addrecord(txtstudentid.text, txtstname.text); studentdetailsform.showdialog(); }
Comments
Post a Comment