【问题标题】:Textbox filter through SQL Datagridview - displaying too many columns (Duplicate Columns)通过 SQL Datagridview 过滤文本框 - 显示太多列(重复列)
【发布时间】:2017-12-25 04:26:12
【问题描述】:

搜索方法在这里:

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();
    }

谢谢

【问题讨论】:

  • 看起来您在设置数据源之前已经在 datagridview 中定义了这些列。在您运行这部分代码之前,datagridview 是什么样的?注释掉代码,运行程序,dgv还有其他4列吗?
  • imgur.com/a/G3oc4 以上是我搜索前的dataGridView1列和datagridview的图片

标签: c# mysql search datagridview filter


【解决方案1】:

好的,所以您正在其他地方填充 datagridview。您只需要将 rowfilter 应用于 textbox_textchanged 事件中的视图

在您填充当前数据网格视图的位置,确保您在更广泛的范围内实例化了 dt,以便文本框事件可以访问它,然后您在 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);

    }

当然,您确实需要切换到 using 语句,以便正确处理使用的对象,但这是为了更多地表明您的网格这样做的基本原因是您正在将另一个数据源应用到网格中并且它不知道您仍然希望您之前拥有的所有信息仅限于与您的过滤器匹配的行。

编辑 - 澄清

让我们这样做。在您的 loaddata 代码中,为每个循环标记整个内容。添加以下行

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

这将暂时隐藏您预先存在的列,而无需您手动执行此操作。 并且应该显示来自查询的所有信息的网格。

然后在您的 textchanged 事件中备注您的所有代码并将其替换为我上面显示的使用数据表 (dt) 的 DefaultView 的行

这应该可以让您启动并运行。完成后,我们可以对查询进行更改,使您可以显示“Active”/“InActive”,而不是位字段的复选框。

【讨论】:

  • 对不起,伙计,不完全明白。能否请您看一下原始帖子中的更新。
  • 与其遍历您填充的数据表,不如将其分配为数据源。然后只需过滤其默认视图即可。
  • 我可以举个例子吗?我不太熟悉的所有编码语言,问我这些行是做什么的,我可以告诉你,我有点理解它们的作用,但我对整个 sql/data 的东西还是陌生的。真的很感谢你帮助!
  • 很高兴听到。您对 ProductStatus 的复选框感到满意吗?保留它们将缓解您以后可能希望通过网格更新更改的任何问题。您可以将该字段重命名为 ProductActive 之类的名称,以便检查假设为是。
猜你喜欢
  • 2013-01-31
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多