2017-07-19 83 views
0

的搜索方法是在這裏:文本框過濾器 - 顯示太多的列(重複列)

private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection("Data Source=DESKTOP-HNR3NJB\\mysql;Initial Catalog=stock;Integrated Security=True"); 
     SqlDataAdapter sda = new SqlDataAdapter("SELECT ProductName FROM [stock].[dbo].[Products]", con); 
     sda.Fill(dt); 
     dataGridView1.DataSource = dt; 
     dt.DefaultView.RowFilter = string.Format("ProductName LIKE '%{0}%'", textBox1.Text); 
    } 

現在,它過濾掉,結果在表中,但它增加了列像下面的圖片:

Search Results 加載數據功能(被儘快加載窗體稱爲:

public void LoadData() 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-HNR3NJB\mysql;Initial Catalog=stock;Integrated Security=True"); 
     con.Open(); 
     //reading data from sql 
     SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [stock].[dbo].[Products]", con); 
     dt = new DataTable(); 
     sda.Fill(dt); 
     dataGridView1.Rows.Clear(); 
     foreach (DataRow item in dt.Rows) 
     { 
      int n = dataGridView1.Rows.Add(); 
      dataGridView1.Rows[n].Cells[0].Value = item["ProductID"].ToString(); 
      dataGridView1.Rows[n].Cells[1].Value = item["ProductName"].ToString(); 
      if ((bool)item["ProductStatus"]) 
      { 
       dataGridView1.Rows[n].Cells[2].Value = "Active"; 
      } 
      else 
      { 
       dataGridView1.Rows[n].Cells[2].Value = "Inactive"; 
      } 
      dataGridView1.Rows[n].Cells[3].Value = item["Employee"].ToString(); 
      dataGridView1.Rows[n].Cells[4].Value = item["CPU"].ToString(); 
      dataGridView1.Rows[n].Cells[5].Value = item["RAM"].ToString(); 
      dataGridView1.Rows[n].Cells[6].Value = item["SSD"].ToString(); 
      dataGridView1.Rows[n].Cells[7].Value = item["HDD"].ToString(); 
      dataGridView1.Rows[n].Cells[8].Value = item["WindowsVersion"].ToString(); 
      dataGridView1.Rows[n].Cells[9].Value = item["Description"].ToString(); 
      dataGridView1.Rows[n].Cells[10].Value = item["Type"].ToString(); 
     } 
     con.Close(); 
    } 

感謝

+0

它幾乎出現像你在DataGridView設置DataSource之前定義的那些列。在運行這段代碼之前,datagridview看起來像什麼?註釋掉代碼,運行程序,dgv是否有其他4列? –

+0

http://imgur.com/a/G3oc4 上面是我的dataGridView1列和datagridview的圖片在我搜索 –

回答

0

OK,所以你在其他地方填充datagridview的。您只需要將rowfilter應用於textbox_textchanged事件中的視圖即可

在填充當前datagridview的位置,請確保將dt在更廣的範圍內實例化,以便textbox事件可以訪問它,然後您在textchanged事件中應該做的所有事情是以下行:

dt.DefaultView.RowFilter = string.Format("ProductName LIKE '%{0}%'", textBox1.text); 

這應該將行限制爲當前找到的行。這裏有一個演示數據庫的例子(你將不得不更改爲滿足您的需求)

DataTable dt; //declared outside a method so that multiple methods have access to it object. 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     //Some other area where the datagridview is populated with more information 
     SqlConnection con = new SqlConnection(@"MyConnectionString"); 
     con.Open(); 
     SqlDataAdapter sda = new SqlDataAdapter("SELECT FirstName, LastName, Address, State FROM Employee", con); 
     dt = new DataTable(); 
     sda.Fill(dt); 
     dataGridView1.DataSource = dt; 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     //all that should be needed to filter the datagridview to your condition 
     dt.DefaultView.RowFilter = string.Format("FirstName LIKE '%{0}%'", textBox1.Text); 

    } 

當然,你真的需要切換到使用語句,以便使用正確處置的對象,但是這是表明更多的網格是這樣做的根本原因是,你所申請的另一個數據源併入電網,它不知道你還想要你之前曾經僅僅侷限於以匹配您的過濾器行的信息。

編輯 - 對於澄清

讓我們做到這一點。在你的loaddata代碼中,爲每個循環指出整個代碼。添加以下行

datagridview1.columns.clear(); 
datagridview1.datasoure = dt; 

這將隱藏您的預先存在的列現在,您無需做手工。 並應該顯示網格與查詢中的所有信息。

然後在你的TextChanged事件的話所有的代碼,並與我行上面顯示替換它使用的數據表(DT)的默認視圖

這應該讓你和運行。完成後,我們可以對查詢進行更改,以便顯示「活動」/「InActive」而不是位域的複選框。

+0

Im很抱歉,隊友,不完全理解。你可以看看原始文章的更新。 –

+0

而不是迭代你填充的數據表,爲什麼不把它分配爲數據源。那麼這只是過濾其默認視圖的問題。 –

+0

我可以舉個例子嗎?所有編碼語言我都不是那麼熟悉,問我所有這些行都做了什麼,我可以告訴你,我有點理解他們做了什麼,但我仍然對整個SQL /數據事物不熟悉。真的很感謝你幫幫我! –