c# - Adding data to DataSource from UI with BindingSource -


i have form (form1) contains grid, bound datasource via bindingsource.

i have button, once clicked opens form (form2) should let user enter new data.

i pass bindingsource form1 form2, , goal once user "saves" input in form2, it'll automatically added form1.

is there way this, w/o directly accessing ui controls?

i.e -

public partial class form2 : form {     bindingsource bs = new bindingsource();     public form2(bindingsource bindingsourcefromform1)             {                 initializecomponent();                 this.bs = bindingsourcefromform1;             }         private void button1_click(object sender, eventargs e)             {                 datarow dr = (this.bs.datasource datatable).newrow();                 dr["col1"] = this.textbox1.text;                 dr["col2"] = this.textbox2.text;                 this.bs.add(dr);             } } 

is there way bind controls on form2 (in example above textbox1/2) bindingsource, , have automatically add values in textbox1 & 2?

something calling this.bs.add(), add() knows take it's values without me explicitly telling go textboxes, since it's bound aforementioned controls?

thanks!

if add bindingsource form designer do, set datasource same source can bind textboxes.

in specialized constructor following code adds new record datasource of form1, assigns datasource datasource of bindingesource instance of form , sets position. new form enable user enter values in new object.

public form2(bindingsource bindingsourcefromform1)     : this() {     bindingsourcefromform1.addnew();     this.bindingsource1.datasource = bindingsourcefromform1.datasource;     this.bindingsource1.position = bindingsourcefromform1.position; } 

if user can cancel operation have compensate calling removecurrent on bindingsourcefromform1 leave excersice not clear if want/need that.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -