c# - Insert to datagridview when SELECT query has WHERE condition -
hi use code load , insert data table using datagridview in c# windows application.
sqlcommand scommand; sqldataadapter sadapter; sqlcommandbuilder sbuilder; dataset sds; datatable stable; private void form1_load(object sender, eventargs e) { string connectionstring = "data source=.\\sqlexpress;attachdbfilename=|datadirectory|\\database1.mdf;integrated security=true;user instance=true"; string sql = "select * mytable"; sqlconnection connection = new sqlconnection(connectionstring); connection.open(); scommand = new sqlcommand(sql, connection); sadapter = new sqldataadapter(scommand); sbuilder = new sqlcommandbuilder(sadapter); sds = new dataset(); sadapter.fill(sds, "mytable"); stable = sds.tables["mytable"]; connection.close(); datagridview1.datasource = sds.tables["mytable"]; datagridview1.readonly = true; save_btn.enabled = false; datagridview1.selectionmode = datagridviewselectionmode.fullrowselect; } private void new_btn_click(object sender, eventargs e) { datagridview1.readonly = false; save_btn.enabled = true; new_btn.enabled = false; delete_btn.enabled = false; } private void delete_btn_click(object sender, eventargs e) { if (messagebox.show("are sure?", "delete", messageboxbuttons.yesno) == dialogresult.yes) { datagridview1.rows.removeat(datagridview1.selectedrows[0].index); sadapter.update(stable); } } private void save_btn_click(object sender, eventargs e) { sadapter.update(stable); datagridview1.readonly = true; save_btn.enabled = false; new_btn.enabled = true; delete_btn.enabled = true; } }
it's ok , work, when try work query have condition no row add datagrid , mytable anymore
sql = "select * mytable col2 = 1";
Comments
Post a Comment