c# - DataGridView retaining old columns -
i can't quite seem figure 1 out. i'm using datagridview in vs2013. i've got simple script pull tables , update grid view. seems work fine except datagridview seems appending columns instead of replacing them new ones.
i've looked around hours , have tried many remedies. perhaps can help?
here's code use far:
public centralstation(mysqlconnection _myconnection) { initializecomponent(); myconnection = _myconnection; myadapter = new mysqldataadapter(); mycommand = new mysqlcommand(" ", myconnection); mydatatable = new datatable(); mybinder = new bindingsource(); populatetableselection(); } private void button1_click(object sender, eventargs e) { try { mycommand.commandtext = "select * tcpro." + this.tablemenulist.text + ";"; myadapter.selectcommand = mycommand; mydatatable.clear(); myadapter.fill(mydatatable); mybinder.datasource = null; mybinder.datasource = mydatatable; datagridview1.datasource = mybinder; myadapter.update(mydatatable); } catch (exception ex) { messagebox.show(ex.message); } }
mycommand.commandtext = "select * tcpro." + this.tablemenulist.text + ";";
mycommand
selects data different db tables, right? tables have different columns
myadapter
fills same table
myadapter.fill(mydatatable);
data adapter doesn't clear rows , need mydatatable.clear();
that
so data adapter doesn't clear columns , need clear them too:
mydatatable.clear(); mydatatable.columns.clear();
it not datagridview fault
Comments
Post a Comment