2016-08-05 2 views
0

이것은 내 코드가 실제로 어떻게 보이고 수행되는지입니다. 필자는 데이터베이스에서 채운 콤보 상자에 따라 데이터를 필터링 한 다음 DataGrid 뷰에 데이터를 표시하려고했습니다. 필자는 코딩의 초보자이기 때문에 콤보 박스 채우기 코드를 작성하는 것이 정말 어려웠습니다. 인터넷에서 실제로 검색 한 결과 대부분의 책을 읽었습니다. 모든 선택이 완료되고 텍스트가 텍스트 상자에 작성되고 검색 버튼 (내가 생성 한)을 에 따라 클릭하면 DataGridview에 표시되는 방식으로이 작업을 수행 할 수 있습니까?동일한 데이터베이스에서 채워진 combobox로 datagridview 필터링

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

관련 문제