sql - How to properly use a DataGridView in C# -
well, very, broad question. i've done lot of research, , i'm still confused.
so, problem have little c# program connected sql database. through c# interface can insert data sql database (i know because checked in mysqlworkbench, , data there), want see it, edit, update , erase needed; , need use datagridview, so, complicated, i've seen lot of info, , confuses me lot more.
i'm gonna almost-complete copypaste of c# program:
private void form1_shown(object sender, eventargs e) { conexion.open(); textbox2.focus(); try { dataset ds = new dataset(); mysqldataadapter da = new mysqldataadapter("select cveestado, nombre tbestados", conexion); da.fill(ds, "filldropdown"); combobox1.displaymember = "nombre"; combobox1.valuemember = "cveestado"; combobox1.datasource = ds.tables["filldropdown"]; conexion.close(); } catch (exception ex) { messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.error); } } private void button1_click(object sender, eventargs e) { guardar(); } private void guardar() { if (textbox2.text == "") { messagebox.show("ingrese un nombre", "error"); textbox2.focus(); } else if (combobox1.selecteditem.tostring() == "") { messagebox.show("elija un estado", "error"); combobox1.focus(); } else { conexion.open(); try { cmd.commandtext = "insert tbmunicipios (nombre, cveestado) values ('" + textbox2.text + "', '" + combobox1.selectedvalue.tostring() + "')"; cmd.executenonquery(); cmd.clone(); messagebox.show("datos guardados", "mensaje"); conexion.close(); textbox2.text = ""; combobox1.text = ""; textbox2.focus(); } catch (exception ex) { messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.error); } } }
with above display "nombre" {name}, obtain "clave" {id}, in combobox; , want same datagridview; repeat, i´ve seen lot of information, confused me lot more.
and, clear, i'm gonna copy-paste sql code too:
create table tbestados ( cveestado int not null, nombre varchar (45) not null, constraint pkcveestado primary key (cveestado) )engine=innodb; create table tbmunicipios ( cvemunicipio int not null auto_increment, nombre varchar (45) not null, cveestado int not null, constraint pkcvemunicipio primary key (cvemunicipio), constraint fkcveedo foreign key (cveestado) references tbestados (cveestado) )engine=innodb;
thanks in advance answers :d
bro recommend use datatable
instead of dataset
, drag datagridview
on ui
private void form1_shown(object sender, eventargs e) { conexion.open(); textbox2.focus(); try { datatable dt = new datatable(); mysqldataadapter da = new mysqldataadapter("select cveestado, nombre tbestados", conexion); da.fill(dt); //here comes datagridview datagridview1.datasource = dt; conexion.close(); } catch (exception ex) { messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.error); } }
and best practice doing call method in form_load
event
Comments
Post a Comment