【问题标题】:Textbox search in DatagridView & FIlters it ContentsDatagridView 中的文本框搜索 & 过滤它的内容
【发布时间】:2016-03-08 22:45:11
【问题描述】:

以下代码有效,但我正在寻找更复杂的内容,即当我在定义的文本框中键入第二名或姓氏的字符时,整个名称(名字、第二名或姓氏)在单个列中( “Nombre_Doctor”)在数据库中,所以它只是过滤它搜索所有列中的单个字符。

或者当我在整个表上放置任何值(数字或字符)并对其进行过滤(再次填充 DataGridView)时如何更改它,因为在代码中您可以看到它选择 *(全部)来自 DOCTORS (table) WHERE Nombre_Doctor (只有一列) ...等

非常感谢您。

LC

private void txtsearchpa_KeyUp(object sender, KeyEventArgs e)
{
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "Select * from [DOCTORS] where Nombre_Doctor like ('" + txtsearchdr.Text + "%')";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    DGV1.DataSource = dt; //DGV = DataGridView

    con.Close();  
} 

【问题讨论】:

  • 所以你想在TextBox 中输入“文本”,并且网格应该过滤掉所有行,除了“文本”出现在 any 列中,对吗?跨度>
  • 不,它必须过滤该列中[TextBox]的任何“文本”,甚至必须过滤所有列中[TextBox]的任何“文本”和/或“数字”。

标签: c# datagridview filter


【解决方案1】:

如果您想做的是根据搜索过滤您的 GridView 数据,我建议这样做: 您有一个包含所有数据的列表或集合。我有这个例子:

   IEnumerable<Product> ListProduct = from Prod in LstProducts where Prod.Description.StartsWith(txtFind.Text) select Prod;
    dataGridView1.Rows.Clear();

    foreach (var item in ListProduct)
        this.dataGridView1.Rows.Add(item.Id, item.Status, item.Description, tipo.Price);

并过滤您想要的列中的信息(描述)并使用您想要的过滤器(StartsWith),但为此您需要一个对象列表(LstProducts)。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2012-07-25
    相关资源
    最近更新 更多