簡體   English   中英

組合框中的選定項目到datagrid C#SQL

[英]Selected Item in combobox to datagrid C# SQL

我們有一個具有不同音樂流派的組合框。 我們希望在組合框中選擇的流派在數據庫中顯示該流派的歌曲,然后將其顯示在數據網格中。

public DataSet sortGenreCBox()
    {
        conn.Open();

        SqlCommand genreBox = new SqlCommand("Select Distinct Genre From Sang", conn);
        SqlDataAdapter adapt = new SqlDataAdapter(genreBox);
        DataSet ds = new DataSet();
        adapt.Fill(ds);

        conn.Close();

        return ds;
    }

該代碼顯示了我們如何從數據庫中提取流派。

public ChooseSong()
    {
        InitializeComponent();

        _DBF = new DatabaseFacade();

        DataSet dsGenreBox = _DBF.sortGenreCBox();
        DataTable dtGenreBox = dsGenreBox.Tables[0];
        sortByGenreCBox.DataContext = dtGenreBox;
        sortByGenreCBox.DisplayMemberPath = dtGenreBox.Columns[0].ToString();

      ...
    }

希望你能幫助:)

您可以從組合框中獲取gerne的名稱,並將該gerne放入SQL查詢中。

創建查詢,如下所示:

 SqlCommand genreBox = new SqlCommand("SELECT DISTINCT " + sortByGenreCBox.selectedItem  + " FROM Sang", conn);

並將datagridview.DataSource設置為DataSet ds。

yourDataGridView.DataSource = ds.tables[0];

編輯:

您可以執行以下操作:

public ChooseSong()
{
   string selectedGerne = sortByGerneCBox.selectedItem.text;
   DataSet ds = DatasortGenreCBox(selectedGerne);

然后,您可以執行以下操作:

       public DataSet sortGenreCBox(string selectedGenre){
 SqlCommand genreBox = new SqlCommand("SELECT DISTINCT " + selectedGenre  + " FROM Sang", conn);
}
public DataSet sortGenreCBox()
    {
        conn.Open();

        SqlCommand genreBox = new SqlCommand("Select Distinct" + sortByGenreCBox.SelectedItem + "from Sang", conn);
        SqlDataAdapter adapt = new SqlDataAdapter(genreBox);
        DataSet ds = new DataSet();
        adapt.Fill(ds);

        conn.Close();

        return ds;
    }

當我們寫它時,它說它在當前內容中不存在?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM