我需要有关C#windows窗体应用程序的帮助。
我在我的表单中创建了一个DataGridView,DataGridView只需显示取决于用户的comobobox选择,组合框有三个选择,每个选择需要显示不同的数据库表,例如,如果他们选择仓库1然后将显示仓库1数据库中的数据库表。如果他们选择仓库2,那么将显示数据库表仓库2和仓库3的相同内容。
每个仓库如果不同的数据库。到目前为止,我只是在没有组合框选择的情况下添加仓库1时工作,但我需要基于组合框选择。
创建每个函数到get数据库表之后,将在每个数据库的form1方法中调用这是我的代码
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");
}
}
答案 0 :(得分:1)
(1)双击组合框。以下代码将出现在您的代码中
private void cmb_DatabaseSelection_SelectedIndexChanged(object sender, EventArgs e)
{
}
将以下代码添加到上述功能中(根据您的需要编码)
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");
}