2016-05-16 78 views
2

我需要你的幫助,我正在做的事情。創建DropDown菜單C#與SQL數據庫表

這很簡單,但它一直在竊聽我。

我在WPF應用程序中創建了一個ComboBox [DropDown Menu],我想用我的數據庫中的所有當前表格填充它。

這就是我正在努力做的事: 當我點擊組合框時,它將顯示數據庫中的所有可用表。然後,當我點擊其中一個時,它會顯示包含在我放置在菜單下的DataGrid中選定表格中的信息。

My Application

,這裏是組合框打開時,我使用的代碼:

private void tableComboBox_DropDownOpened(object sender, EventArgs e) 
    { 
     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) 
     { 
      tableComboBox.Items.Add(row); 
     } 
    } 

我已經看過並嘗試了一些不同的方法,但其中的非正常工作。 我試圖在DataGrid中顯示錶格的內容,但我又被卡住了。

請同仁編碼。幫助這個新手在這裏! :)

回答

2

所以這就是我很快想出的。

  1. 刪除tableComboBox_DropDownOpened事件。
  2. 添加事件comboBox_SelectionChange
  3. 更改您的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; 
     } 
    } 
} 
}  
+0

雖然它在某種程度上起作用,但在我改變了一些東西之後,Doa Ink仍然存在一個問題。我首先注意到它不會正確加載數據。這是圖像: http://imgur.com/U8jCFCZ 當我選擇一個項目時,組合框進入其默認階段沒有選擇的項目。 是的它確實顯示了dataGrid中的表格,但並不像您在下面的圖片中看到的那樣。 我已經嘗試了幾個更正,但他們都沒有解決。 下面是完整的代碼: http://www.codesend.com/view/079261ba7c6be8bb8ceae6debc56d30b/再次 感謝您的時間DOA –

0

這是在組合框存儲DISPLY命名代碼和值

private void Form1_Load(object sender, EventArgs e) 
    { 

     var Header = new BindingList<KeyValuePair<string, string>>(); 
     List<string> LV = ListHeader(); 
     foreach (var listOut in LV) 
     { 
      Header.Add(new KeyValuePair<string, string>(listOut, "val"+listOut)); 
     } 
     tableComboBox.DataSource = Header; 
     tableComboBox.DisplayMember = "Key"; 
     tableComboBox.ValueMember = "Value"; 
    } 


public List<string> ListHeader() 
{ 
    List<string> list = new List<String>(); 

    try 
    { 
     cmd.Connection = db.connection; 

     cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE';"; 

     var reader = cmd.ExecuteReader(); 

      for(int i=0;i<reader.FieldCount;i++) 
      { 
      list.Add(reader.GetName(i)); 
      } 

     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Get Header", 
      MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    finally 
    { 
     con.Close(); 
    } 

    return list; 
} 

你可以得到組合框的值是這樣的:

Console.WriteLine(( (KeyValuePair)tableComboBox.SelectedItem).Value.ToString());

或者你只是想顯示名稱:

tableComboBox.SelectedItem。ToString()