[英]C# Show DataGridView based on the combobox selection
我需要有關 C# windows 窗體應用程序的幫助。
我在表單中創建了一個 DataGridView,DataGridView 只需要根據用戶的組合框選擇顯示,組合框有三個選擇,每個選擇都需要顯示不同的數據庫表,例如,如果他們選擇了倉庫 1,則將顯示來自倉庫 1 數據庫。 如果他們選擇倉庫 2,那么將顯示數據庫表倉庫 2 和倉庫 3 的相同內容。
每個倉庫如果不同的數據庫。 到目前為止,我只在沒有組合框選擇的情況下添加倉庫 1 時就可以工作,但我需要基於組合框選擇。
創建到獲取數據庫表的每個函數后,將在每個數據庫的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");
}
}
(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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.