2017-07-19 56 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 /数据事物不熟悉。真的很感谢你帮帮我! –