Reputation: 159
I need help with my C# windows form application.
I have one DataGridView created in my form, the DataGridView needs to only show depending of the users comobobox selection, the combobox has three selection and each selection needs to show a different database table for example if they selection warehouse1 then will show the database table from warehouse1 database. if they selection warehouse2 then will show the database table warehouse2 and same thing for warehouse3.
each warehouse if different database. so far I have it working when just adding the warehouse1 without the combobox selection, but I need to be based on the combobox selection.
after creating the each function to the get database table will be called in the form1 method for each database
here is my code
private void Form1_Load(object sender, EventArgs e)
{
// Bind the DataGridView to the BindingSource
// and load the data from the database.
if (cmb_DatabaseSelection.SelectedItem == "warehouse1")
{
dataGridView_ShowAllData.DataSource = bindingSource;
GetLeMarsConnectionDatabaseConnection("Select * from dbo.AllInvoicesInReadyStatus");
}
if (cmb_DatabaseSelection.SelectedItem == "warehouse2")
{
dataGridView_ShowAllData.DataSource = bindingSource;
GetLeMarsConnectionDatabaseConnection("Select * from dbo.AllInvoicesInReadyStatus");
}
if (cmb_DatabaseSelection.SelectedItem == "warehouse3")
{
dataGridView_ShowAllData.DataSource = bindingSource;
GetLeMarsConnectionDatabaseConnection("Select * from dbo.AllInvoicesInReadyStatus");
}
this.rpt_CallSSRSReport.RefreshReport();
CallReport();
}
private void GetLeMarsConnectionDatabaseConnection(string selectCommand)
{
try
{
//Create the connection string, data adapter and data table.
String connectionString = "database1";
// Specify a connection string. Replace the given value with a
// database accessible to your system.
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
System.Data.DataTable table = new System.Data.DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView_ShowAllData.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show("Database connection ERROR");
}
}
private void GetSchuylerConnectionDatabaseConnection(string selectCommand)
{
try
{
//Create the connection string, data adapter and data table.
String connectionString = "database2";
// Specify a connection string. Replace the given value with a
// database accessible to your system.
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
System.Data.DataTable table = new System.Data.DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView_ShowAllData.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show("Database connection ERROR");
}
}
private void GetDetroitLakesConnectionDatabaseConnection(string selectCommand)
{
try
{
//Create the connection string, data adapter and data table.
String connectionString = "database3";
// Specify a connection string. Replace the given value with a
// database accessible to your system.
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
System.Data.DataTable table = new System.Data.DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView_ShowAllData.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show("Database connection ERROR");
}
}
Upvotes: 1
Views: 1325
Reputation: 1496
(1) Double Click on Combo box. Following code will appear in your code behind
private void cmb_DatabaseSelection_SelectedIndexChanged(object sender, EventArgs e)
{
}
or u can add event like this as well
Add the following code to the above function (Code as per your needs)
if (cmb_DatabaseSelection.SelectedItem == "warehouse1")
{
dataGridView_ShowAllData.DataSource = bindingSource;
GetLeMarsConnectionDatabaseConnection("Select * from dbo.AllInvoicesInReadyStatus");
}
if (cmb_DatabaseSelection.SelectedItem == "warehouse2")
{
dataGridView_ShowAllData.DataSource = bindingSource;
GetLeMarsConnectionDatabaseConnection("Select * from dbo.AllInvoicesInReadyStatus");
}
if (cmb_DatabaseSelection.SelectedItem == "warehouse3")
{
dataGridView_ShowAllData.DataSource = bindingSource;
GetLeMarsConnectionDatabaseConnection("Select * from dbo.AllInvoicesInReadyStatus");
}
Upvotes: 1