vb.net - Combobox databinding through code -
vb.net - Combobox databinding through code -
i have form binding info controls. code
private sub form1_load(sender system.object, e system.eventargs) handles mybase.load seek conn = new oledbconnection("provider=microsoft.ace.oledb.12.0;;data source=c:\users\desraj\desktop\mydb.accdb;") conn.open() formload() adap = new oledbdataadapter("select * mytable", conn) dim ds new dataset adap.fill(ds, "mytable") bindingsource1.datasource = ds.tables(0) bindingnavigator1.bindingsource = bindingsource1 textbox1.databindings.add("text", bindingsource1, "id") textbox2.databindings.add("text", bindingsource1, "myname") combobox1.databindings.add("text", bindingsource1, "city_code") combobox2.databindings.add("text", bindingsource1, "state_code") grab ex exception msgbox(ex.message.tostring) end seek end sub public sub formload() seek cmd = new oledbcommand("select * mycity", conn) reader = cmd.executereader while reader.read = true combobox1.items.add(reader.item(1)) end while reader.close() cmd = new oledbcommand("select * mystate", conn) reader = cmd.executereader while reader.read = true combobox2.items.add(reader.item(1)) end while reader.close() grab ex exception msgbox(ex.message) end seek end sub
have 3 tables table 1: mytable
4 columns id,myname,city_code(fk),state_code(fk)
table 2: mycity
2 columns city_code,city
table 3: mystate
2 columns state_code,state
example: mytable
has next values id
myname
city_code
state_code
1 alan 2 3 2 stone 3 1 3 brock 1 2 mycity
has next values city_code
city
1
abc
2
xyz
3
pqr
mystate
has next values state_code
state
1
lmn
2
fgh
3
def
the problem instead of loading combobox1
, combobox2
city , state, loads city_code
, state_code
. how can load city
, state
in combobox1
, combobox2
, having foreign keys in mytable
?
you should first binding parent list combobox setting displaymember, valuemember , datasource. valuemember name of pk. utilize databindings bind selectedvalue fk column of kid table, e.g.
with citycombobox .displymember = "city" .valuemember = "city_code" .datasource = citybindingsource .databindings.add("selectedvalue", personbindingsource, "city_code") end
note 2 different bindingsources used.
vb.net combobox
Comments
Post a Comment