mysql - Use of Combobox in C# using SQL -
first of all, in advance answer. i'm complete noob inn c#, , far it's interesting, yet complicated. problem this:
i connected sql database c#, display "name" column in combobox, need use "id" column. guess it's more understandable code.
(i'm not going put code of connection, because think irrelevant).
private void form1_shown(object sender, eventargs e) { conexion.open(); mysqlcommand cm = new mysqlcommand("select * tbestados", conexion); try { mysqldatareader dr = cm.executereader(); while (dr.read()) { combobox1.items.add(dr["nombre"]); } dr.close(); dr.dispose(); } catch (exception ex) { messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.error); } conexion.close(); } private void button1_click(object sender, eventargs e) { //when press button, want display "id" of table messagebox.show(convert.tostring(combobox1.valuemember)); }
i'm gonna add of mysql statements go little further.
insert tbestados (cveestado, nombre) values (1, "aguascalientes"); insert tbestados (cveestado, nombre) values (2, "baja california"); insert tbestados (cveestado, nombre) values (3, "baja california sur");
so, if choose "baja california sur" combobox, want button show "3".
thanks!
i hope combobox code looks :
<combobox name="combobox1" width="120" height="22" itemssource="{binding}"/>
and update code:
private void form1_shown(object sender, eventargs e) { conexion.open(); try { mysqldataadapter da = new mysqldataadapter("select * tbestados", conexion); dataset ds = new dataset(); da.fill(ds, "tbestados"); combobox1.itemssource = ds.tables[0].defaultview; combobox1.displaymemberpath = ds.tables[0].columns["nombre"].tostring(); combobox1.selectedvaluepath = ds.tables[0].columns["cveestado"].tostring(); } catch (exception ex) { messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.error); } conexion.close(); }
and update vbutton click :
private void button1_click(object sender, routedeventargs e) { messagebox.show("selected nombre="+combobox1.text+" , cveestado="+ combobox1.selectedvalue.tostring()); }
Comments
Post a Comment