所以這就是我很快想出的。
- 刪除tableComboBox_DropDownOpened事件。
- 添加事件comboBox_SelectionChange
- 更改您的db連接字符串,combobox和dataGrid的名稱以匹配您的。
以下是下面的代碼,我將loadCombo()移動到了初始化的下方,以使其變得簡單。
public partial class MainWindow : Window
{
SqlConnection db = new SqlConnection("Your Connection String Here");
public MainWindow()
{
InitializeComponent();
loadCombo();
}
private void loadCombo()
{
SqlCommand cmd = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE';", db);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
foreach (DataRow row in dataSet.Tables[0].Rows)
{
comboBox.Items.Add(row[0]);
}
}
private DataTable loadDataGrid(String inTableName)
{
SqlCommand cmd = new SqlCommand("SELECT COLUMN_NAME,* FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '"+ inTableName + "';", db);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet.Tables[0];
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string text = e.AddedItems[0].ToString(); ;
dataGrid.ItemsSource = loadDataGrid(text).DefaultView;
}
}
希望這有助於
我在下面更新您的代碼。把它粘貼一下就可以了。我不確定創建按鈕發生了什麼,但讓我們看看我們是否可以修復組合框和數據網格。我在代碼中添加了一些註釋來幫助解釋我的理性。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DatabaseManagement
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Database db = new Database();
public MainWindow()
{
InitializeComponent();
// Add the loadCombo back
loadCombo();
// comment this out until you get the desired functionality
//TableCreateGrid.Visibility = Visibility.Hidden;
}
private void createButton_Click(object sender, RoutedEventArgs e)
{
try
{
// I am not sure what you are doing here -
if (string.IsNullOrEmpty(column3TextBox.Text) && string.IsNullOrEmpty(column4TextBox.Text))
{
db.CreateTable(tableTextBox.Text, column1TextBox.Text, column2TextBox.Text);
informationBlock.Text = db.infoBoxString;
}
else if (string.IsNullOrEmpty(column4TextBox.Text))
{
db.CreateTable(tableTextBox.Text, column1TextBox.Text, column2TextBox.Text, column3TextBox.Text);
informationBlock.Text = db.infoBoxString;
}
else if (!string.IsNullOrEmpty(column3TextBox.Text) && !string.IsNullOrEmpty(column4TextBox.Text))
{
db.CreateTable(tableTextBox.Text, column1TextBox.Text, column2TextBox.Text, column3TextBox.Text, column4TextBox.Text);
informationBlock.Text = db.infoBoxString;
}
}
catch (Exception ex)
{
informationBlock.Text = ex.Message;
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
db.Connect();
informationBlock.Text = db.infoBoxString;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
db.Close();
informationBlock.Text = db.infoBoxString;
}
private void loadCombo()
{
SqlCommand cmd = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE';", db.connection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
foreach (DataRow row in dataSet.Tables[0].Rows)
{
tableComboBox.Items.Add(row[0]);
}
}
private DataTable loadDataGrid(String inTableName)
{
// Here you need to specify the columns you want in the TableCreateGrid
// example this just will show the COLUMN NAME,DATA TYPE, CHARACTER MAXIMUM LENGTH and so on
// SqlCommand cmd = new SqlCommand("SELECT COLUMN_NAME,DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, TABLE_SCHEMA FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + inTableName + "';", db.connection);
SqlCommand cmd = new SqlCommand("SELECT COLUMN_NAME,* FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + inTableName + "';", db.connection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet.Tables[0];
}
private void tableComboBox_DropDownOpened(object sender, EventArgs e)
{
//loadCombo();
// dont need since this is loaded on Initialize
}
private void tableComboBox_DropDownClosed(object sender, EventArgs e)
{
// tableComboBox.Items.Clear();
// dont need since this will clear all the items in the tableComboBox
}
private void tableComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
try
{
string text = e.AddedItems[0].ToString(); ;
dataGrid.ItemsSource = loadDataGrid(e.AddedItems[0].ToString()).DefaultView;
}
catch (Exception ex)
{
informationBlock.Text = ex.Message;
}
}
}
}
雖然它在某種程度上起作用,但在我改變了一些東西之後,Doa Ink仍然存在一個問題。我首先注意到它不會正確加載數據。這是圖像: http://imgur.com/U8jCFCZ 當我選擇一個項目時,組合框進入其默認階段沒有選擇的項目。 是的它確實顯示了dataGrid中的表格,但並不像您在下面的圖片中看到的那樣。 我已經嘗試了幾個更正,但他們都沒有解決。 下面是完整的代碼: http://www.codesend.com/view/079261ba7c6be8bb8ceae6debc56d30b/再次 感謝您的時間DOA –