c# - Putting Values in Controls when dynamically creating a form -
i have been working on issue while , figured out. trying set control when form being initialize. thinking control's value isn't being set because control has been drawn yet (correct me if wrong).
my partial form code
//in form 1 .... private void button2_click(object sender, eventargs e) { form2 form = new form2(); form.owner = this; form.show(this); } ....} public form2() { initializecomponent(); setdata(); //sets datagridview's combobox column , databinds datatable setgrid(); //sets each row in datagridview combobox's value string } private void setdata() { gvtest.allowusertoaddrows = false; string strconn = "server=10.253.3.185;database=petersun-test1;user id=ctore;password=cqi$$;connection timeout=30"; sqlconnection conn = new sqlconnection(strconn); datatable dit = new datatable(); try { conn.open(); string sql = "select ltrim(rtrim(column_name)) columnname information_schema.columns table_name='coproc' order ordinal_position"; sqlcommand cmd = new sqlcommand(sql, conn); sqldataadapter da = new sqldataadapter(cmd); da.fill(dit); datagridviewcomboboxcolumn dcdatabasefields = new datagridviewcomboboxcolumn(); dcdatabasefields.datapropertyname = "columnname"; dcdatabasefields.headertext = "database field name"; dcdatabasefields.name = "dbfields"; dcdatabasefields.displaymember = "columnname"; dcdatabasefields.valuemember = "columnname"; dcdatabasefields.width = 200; bindingsource bsourcefields = new bindingsource(); bsourcefields.datasource = dit; dcdatabasefields.datasource = bsourcefields; dcdatabasefields.datasource = dit; // bsourcefields; gvtest.columns.add(dcdatabasefields); } { conn.close(); } datatable dt = new datatable(); datacolumn dc = new datacolumn("options"); dt.columns.add(dc); datarow dr = dt.newrow(); dr["options"] = "a"; dt.rows.add(dr); dr = dt.newrow(); dr["options"] = "c"; dt.rows.add(dr); dr = dt.newrow(); dr["options"] = "d"; dt.rows.add(dr); dr = dt.newrow(); dr["options"] = "e"; dt.rows.add(dr); gvtest.datasource = dt; } private void setgrid() { foreach(datagridview row in gvgrid.rows) { row.cells[0].value = "string"; } }
so think happening is: 1. component getting initialized 2. creates datagridview columns 3. sets column values 4. draws form
is thinking correct? question form event should set control's value? did research , thinking on paint event, not sure either. explain form's life cycle in detail or point me somewhere. thanks
i added coded form_load , added values grid
Comments
Post a Comment