2016-08-05 9 views
0

これは私のコードが実際にどのように見えて実行されるかです。私は、データベースから入力したコンボボックスに従ってデータをフィルタリングし、データグリッドビューにデータを表示しようとしています。私はコーディングの初心者であるため、コンボボックスのコードを記述するのは本当に難しいです。私はインターネットで実際に検索し、ほとんどのタイトルを読んでいます。すべての選択が完了し、テキストがテキストボックスに書き込まれ、検索ボタン(私が作成した)が のデータグリービューの選択に従ってクリックされた後にこれを行う方法はありますか?同じデータベースからのコンボボックスでデータグリッドビューをフィルタリングする

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace KPI_Tool 
{ 
    public partial class SearchForm : Form 
    { 
     static SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\010495\Desktop\KPI_Tool\KPI_Tool\KPI_Store.mdf;Integrated Security=True");  

     public SearchForm() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
// TODO: This line of code loads data into the 'kPI_StoreDataSet1.Store' table. You can move, or remove it, as needed. 
this.myAdapter.Fill(this.myDataSet.Store); 

     } 



     private void Group_DropDown(object sender, EventArgs e) 
     { 

     conn.Open(); 
     comboBox1.Items.Clear(); 
     SqlCommand cmd = conn.CreateCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "SELECT DISTINCT GroupN FROM Store WHERE GroupN IS NOT NULL"; 
     cmd.ExecuteNonQuery(); 
     DataTable dt = new DataTable(); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     da.Fill(dt); 

     foreach (DataRow dr in dt.Rows) 
     { 
      comboBox1.Items.Add(dr["GroupN"].ToString()); 
     } 

     conn.Close(); 


     } 

     private void Tech_DropDown(object sender, EventArgs e) 
     { 

      conn.Open(); 

      comboBox2.Items.Clear(); 

      SqlCommand cmd = conn.CreateCommand(); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "SELECT DISTINCT Tech_Area FROM Store WHERE Tech_Area IS NOT NULL"; 
      cmd.ExecuteNonQuery(); 
      DataTable dt = new DataTable(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(dt); 

      foreach (DataRow dr in dt.Rows) 
      { 
       comboBox2.Items.Add(dr["Tech_Area"].ToString()); 
      } 

      conn.Close(); 

     } 

     private void Level_DropDown(object sender, EventArgs e) 
     { 
      conn.Open(); 

      comboBox3.Items.Clear(); 

      SqlCommand cmd = conn.CreateCommand(); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "SELECT DISTINCT LevelOf FROM Store WHERE LevelOf IS NOT NULL"; 
      cmd.ExecuteNonQuery(); 
      DataTable dt = new DataTable(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(dt); 

      foreach (DataRow dr in dt.Rows) 
      { 
       comboBox3.Items.Add(dr["LevelOf"].ToString()); 
      } 

      conn.Close(); 
     } 

     private void Domain_DropDown(object sender, EventArgs e) 
     { 
      conn.Open(); 

      comboBox4.Items.Clear(); 

      SqlCommand cmd = conn.CreateCommand(); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "SELECT DISTINCT DomainN FROM Store WHERE DomainN IS NOT NULL"; 
      cmd.ExecuteNonQuery(); 
      DataTable dt = new DataTable(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(dt); 

      foreach (DataRow dr in dt.Rows) 
      { 
       comboBox4.Items.Add(dr["DomainN"].ToString()); 
      } 

      conn.Close(); 
     } 

     private void Type_DropDown(object sender, EventArgs e) 
     { 
      conn.Open(); 

      comboBox5.Items.Clear(); 

      SqlCommand cmd = conn.CreateCommand(); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "SELECT DISTINCT TypeN FROM Store WHERE TypeN IS NOT NULL"; 
      cmd.ExecuteNonQuery(); 
      DataTable dt = new DataTable(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(dt); 

      foreach (DataRow dr in dt.Rows) 
      { 
       comboBox5.Items.Add(dr["TypeN"].ToString()); 
      } 

      conn.Close(); 
     } 

     private void Severity_DropDown(object sender, EventArgs e) 
     { 
      conn.Open(); 

      comboBox6.Items.Clear(); 

      SqlCommand cmd = conn.CreateCommand(); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "SELECT DISTINCT Severity FROM Store WHERE Severity IS NOT NULL"; 
      cmd.ExecuteNonQuery(); 
      DataTable dt = new DataTable(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(dt); 

      foreach (DataRow dr in dt.Rows) 
      { 
       comboBox6.Items.Add(dr["Severity"].ToString()); 
      } 

      conn.Close(); 
     } 

     private void AlertTB_Click(object sender, MouseEventArgs e) 
     { 
      AlertTB.Clear(); 
     } 


     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 

     } 

     private void ListB_Click(object sender, EventArgs e) 
     { 

     } 

     private void ClearB_Clicked(object sender, EventArgs e) 
     { 
      comboBox1.SelectedIndex = -1; 
      comboBox2.SelectedIndex = -1; 
      comboBox3.SelectedIndex = -1; 
      comboBox4.SelectedIndex = -1; 
      comboBox5.SelectedIndex = -1; 
      comboBox6.SelectedIndex = -1; 

      AlertTB.Clear(); 
      AlertTB.Text = "Write Here.."; 

     } 


     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 

     { 
      conn.Open(); 
      myBindingSource.Filter = "GroupN= '{0}'"+comboBox1.SelectedItem.Te; 
      conn.Close(); 
     } 



     } 

    } 

ここは自分のユーザーインターフェイスです。私はVisual Studio Professional 2013を使用しています。非常に基本的な文章で私に説明してください。私は論理、コードの背後にある構造を学びたい。

UI

答えて

0
public static DataSet SQLGetData(string connectionString, string commandString) 
{ 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     DataSet DS = new DataSet(); 
     DataTable DT = new DataTable("Table1"); 
     try 
     { 
      connection.Open(); 
      SqlCommand command = new SqlCommand(commandString, connection); 
      //command.CommandTimeout = 3000; 
      SqlDataReader read = command.ExecuteReader(); 
      DS.Tables.Add(DT); 
      DS.Load(read, LoadOption.PreserveChanges, DS.Tables[0]); 

     } 
     catch (SqlException e) 
     { 
      System.Windows.Forms.MessageBox.Show(e.Message); 
     } 
     finally 
     { 
      connection.Close(); 
     } 
     return DS; 
    } 
} 

private void SetFilter() 
{ 
    string command = "SELECT * FROM Store"; 

    int count = 0; 

    if (comboBox1.Text != "") 
    { 
     command = command + " WHERE GroupN = '" + comboBox1.Text + "'"; 
     count = count + 1; 
    } 

    if (comboBox2.Text != "") 
    { 
     if (count == 0) 
     { 
      command = command + " WHERE Tech_Area = '" + comboBox2.Text + "'";     
     } 
     else 
     { 
      command = command + " AND Tech_Area = '" + comboBox2.Text + "'"; 
     } 
     count = count + 1;     
    } 

    if (comboBox3.Text != "") 
    { 
     if (count == 0) 
     { 
      command = command + " WHERE LevelOf = '" + comboBox3.Text + "'";     
     } 
     else 
     { 
      command = command + " AND LevelOf = '" + comboBox3.Text + "'"; 
     } 
     count = count + 1;     
    } 
    // comboBox4, comboBox5, comboBox6 

    string connStr; //Connection string; 

    DataSet DS = new DataSet(); 
    DS = SQLGetData(connStr, command);  
    DataGridView1.DataSource = DS.Tables[0];    
} 
+0

データアダプタは文字列型を処理できないため、テキストに変換する必要があります。どうすればそれをすることができますか? – Ruveyda

+0

更新されたコード... – DartAlex

関連する問題