在 datagridview 中搜索 C# winfom [英] search in datagridview C# winfom

查看:24
本文介绍了在 datagridview 中搜索 C# winfom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 DataGridView 中放置一个搜索选项,即用户在 TextBox 中输入字符串或 int 以及 DataGridView 应该输入的类似记录被突出显示.我尝试使用 KeyPress 事件但没有奏效.

I want to put a search option in DataGridView, i.e., User types the string or int in TextBox and similar records the DataGridView should be highlighted. I tried it using KeyPress event but didn't work.

  if (Char.IsLetter(e.KeyChar))
  {
     for (int i = 0; i < (dgemployee.Rows.Count); i++)
     {
         if (dgemployee.Rows[i].Cells["Employee"].Value.ToString().
                    StartsWith(e.KeyChar.ToString(), true, 
                    CultureInfo.InvariantCulture))
         {
             dgemployee.Rows[i].Cells[0].Selected = true;
             return;                     
         }
      }

实际上,我需要搜索整个DataGridView,而不仅仅是一列和一行.那么有什么最好的解决方案吗?

Actually, I need to search the whole DataGridView, not only one column and row. So any best solution?

推荐答案

如果你的 DataGridView 绑定到一个 DataTable 或一个 DataView,你可以这样做:

If your DataGridView is bound to a DataTable or a DataView, you can do this:

创建一个 BindingSource 并制作 BindingSource.DataSource 您的 DGV 当前正在使用的数据表或数据视图.然后将您的 DataGridView.DataSource 设置为 BindingSource.然后,您可以使用 BindingSource.Filter 属性通过将 BindingSource.Filter 设置为您的查询字符串来查询您的数据源,这将自动过滤 DGV.您可以在此处找到语法 - 它是与基本的 SQL 查询非常相似,只是您只能在字符串的开头和结尾使用通配符.

Create a BindingSource and make BindingSource.DataSource the Datatable or DataView that your DGV is currently using. Then set your DataGridView.DataSource to the BindingSource. Then you can use the BindingSource.Filter property to query your datasource by setting the BindingSource.Filterto your query string which will automatically filter the DGV. You can find the syntax here - it is very similar to basic SQL queries, except you can only use wild cards on the beginning and end of the string.

这篇关于在 datagridview 中搜索 C# winfom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆