c# - Refreshing multiple datagridviews with single button click -
not sure if question has been asked before, here goes.. have front end app coded in c# windows forms. on second form have 2 datagridviews gets populated 2 different sql server pre-defined views
i need refresh both datagrids @ same time single button click button click looks this..
private void refreshbtn_click(object sender, eventargs e) { sqlconnection myconnection = new sqlconnection("removed illustration only"); string query = "select * daily_orders order orderno desc"; sqlcommand cmd = new sqlcommand(query, myconnection); sqldataadapter da = new sqldataadapter(cmd); datatable dt = new datatable(); da.fill(dt); datagridview1.datasource = dt; }
how understand is, c# opens new connection, queries db , returns filling datagridview1 required data. same click event request data sql view , populate datagridview @ same time. visually both grids aligned vertically on same form, 1 on of other.
many in advance
move code refreshing grid1 separate function. copy paste , duplicate function grid2. change sql grid2 grid2 name. rename copied function 2. add call both functions button click refresh both grids.
private void refreshbtn_click(object sender, eventargs e) { //call both functions refresh both on button click refreshgrid1(); refreshgrid2(); } private void refreshgrid1() { sqlconnection myconnection = new sqlconnection("removed illustration only"); string query = "select * daily_orders order orderno desc"; sqlcommand cmd = new sqlcommand(query, myconnection); sqldataadapter da = new sqldataadapter(cmd); datatable dt = new datatable(); da.fill(dt); datagridview1.datasource = dt; } //give function unique name represent second grid refresh private void refreshgrid2() { sqlconnection myconnection = new sqlconnection("removed illustration only"); string query = "select * daily_orders order orderno desc"; sqlcommand cmd = new sqlcommand(query, myconnection); sqldataadapter da = new sqldataadapter(cmd); datatable dt = new datatable(); da.fill(dt); //rename second grid name datagridview2.datasource = dt; }
Comments
Post a Comment