winforms - C# Show DataGridView based on the combobox selection -
i need c# windows form application.
i have 1 datagridview created in form, datagridview needs show depending of users comobobox selection, combobox has 3 selection , each selection needs show different database table example if selection warehouse1 show database table warehouse1 database. if selection warehouse2 show database table warehouse2 , same thing warehouse3.
each warehouse if different database. far have working when adding warehouse1 without combobox selection, need based on combobox selection.
after creating each function database table called in form1 method each database
here code
private void form1_load(object sender, eventargs e) { // bind datagridview bindingsource // , load data database. if (cmb_databaseselection.selecteditem == "warehouse1") { datagridview_showalldata.datasource = bindingsource; getlemarsconnectiondatabaseconnection("select * dbo.allinvoicesinreadystatus"); } if (cmb_databaseselection.selecteditem == "warehouse2") { datagridview_showalldata.datasource = bindingsource; getlemarsconnectiondatabaseconnection("select * dbo.allinvoicesinreadystatus"); } if (cmb_databaseselection.selecteditem == "warehouse3") { datagridview_showalldata.datasource = bindingsource; getlemarsconnectiondatabaseconnection("select * dbo.allinvoicesinreadystatus"); } this.rpt_callssrsreport.refreshreport(); callreport(); } private void getlemarsconnectiondatabaseconnection(string selectcommand) { try { //create connection string, data adapter , data table. string connectionstring = "database1"; // specify connection string. replace given value // database accessible system. // create new data adapter based on specified query. dataadapter = new sqldataadapter(selectcommand, connectionstring); // create command builder generate sql update, insert, , // delete commands based on selectcommand. these used // update database. sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); // populate new data table , bind bindingsource. system.data.datatable table = new system.data.datatable(); table.locale = system.globalization.cultureinfo.invariantculture; dataadapter.fill(table); bindingsource.datasource = table; // resize datagridview columns fit newly loaded content. datagridview_showalldata.autoresizecolumns(datagridviewautosizecolumnsmode.allcellsexceptheader); } catch (sqlexception) { messagebox.show("database connection error"); } } private void getschuylerconnectiondatabaseconnection(string selectcommand) { try { //create connection string, data adapter , data table. string connectionstring = "database2"; // specify connection string. replace given value // database accessible system. // create new data adapter based on specified query. dataadapter = new sqldataadapter(selectcommand, connectionstring); // create command builder generate sql update, insert, , // delete commands based on selectcommand. these used // update database. sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); // populate new data table , bind bindingsource. system.data.datatable table = new system.data.datatable(); table.locale = system.globalization.cultureinfo.invariantculture; dataadapter.fill(table); bindingsource.datasource = table; // resize datagridview columns fit newly loaded content. datagridview_showalldata.autoresizecolumns(datagridviewautosizecolumnsmode.allcellsexceptheader); } catch (sqlexception) { messagebox.show("database connection error"); } } private void getdetroitlakesconnectiondatabaseconnection(string selectcommand) { try { //create connection string, data adapter , data table. string connectionstring = "database3"; // specify connection string. replace given value // database accessible system. // create new data adapter based on specified query. dataadapter = new sqldataadapter(selectcommand, connectionstring); // create command builder generate sql update, insert, , // delete commands based on selectcommand. these used // update database. sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); // populate new data table , bind bindingsource. system.data.datatable table = new system.data.datatable(); table.locale = system.globalization.cultureinfo.invariantculture; dataadapter.fill(table); bindingsource.datasource = table; // resize datagridview columns fit newly loaded content. datagridview_showalldata.autoresizecolumns(datagridviewautosizecolumnsmode.allcellsexceptheader); } catch (sqlexception) { messagebox.show("database connection error"); } }
(1) double click on combo box. following code appear in code behind
private void cmb_databaseselection_selectedindexchanged(object sender, eventargs e) { }
add following code above function (code per needs)
if (cmb_databaseselection.selecteditem == "warehouse1") { datagridview_showalldata.datasource = bindingsource; getlemarsconnectiondatabaseconnection("select * dbo.allinvoicesinreadystatus"); } if (cmb_databaseselection.selecteditem == "warehouse2") { datagridview_showalldata.datasource = bindingsource; getlemarsconnectiondatabaseconnection("select * dbo.allinvoicesinreadystatus"); } if (cmb_databaseselection.selecteditem == "warehouse3") { datagridview_showalldata.datasource = bindingsource; getlemarsconnectiondatabaseconnection("select * dbo.allinvoicesinreadystatus"); }
Comments
Post a Comment