在datagridview中搜索C#winfom

Nhu*_*ren 3 c# search datagridview winforms

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

  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;                     
         }
      }
Run Code Online (Sandbox Code Playgroud)

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

ale*_*exD 5

如果DataGridView绑定到DataTable或DataView,则可以执行以下操作:

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