针对DataGridView中已进行过数据绑定,即已向DataGridView中添加了一些数据,可以结合Linq查询,并让匹配查询的行高亮显示,如下图:

 

  

 

  具体实现如下:

 

[csharp] view plain copy

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

 

namespace Maxes_PC_Client {

public partial class frmWelcome : Form {

private int beforeMatchedRowIndex = 0;

 

public frmWelcome()

{

InitializeComponent();

}

 

private void frmWelcome_Load(object sender, EventArgs e) {

this.dataGridViewInit();

}

 

/// <summary>

/// DataGridView添加数据、初始化

/// </summary>

private void dataGridViewInit() {

Dictionary<String, String> map = new Dictionary<String, String>();

map.Add("Lily", "22");

map.Add("Andy", "25");

map.Add("Peter", "24");

 

// 在这里必须创建一个BindIngSource对象,用该对象接收Dictionary<T, K>泛型集合的对象

BindingSource bindingSource = new BindingSource();

// 将泛型集合对象的值赋给BindingSourc对象的数据源

bindingSource.DataSource = map;

 

this.dataGridView.DataSource = bindingSource;

}

 

private void SearchButton_Click(object sender, EventArgs e) {

if (this.KeyWord.Text.Equals("")) {

return;

}

 

// Linq模糊查询

IEnumerable<DataGridViewRow> enumerableList = this.dataGridView.Rows.Cast<DataGridViewRow>();

List<DataGridViewRow> list = (from item in enumerableList

where item.Cells[0].Value.ToString().IndexOf(this.KeyWord.Text) >= 0

select item).ToList();

 

// 恢复之前行的背景颜色为默认的白色背景

this.dataGridView.Rows[beforeMatchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.White;

 

if (list.Count > 0) {

// 查找匹配行高亮显示

int matchedRowIndex = list[0].Index;

this.dataGridView.Rows[matchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;

this.beforeMatchedRowIndex = matchedRowIndex;

}

}

}

}

C#使用Linq对DataGridView进行模糊查找的更多相关文章

  1. C# 用Linq查询DataGridView行中的数据是否包含(各种操作)

    http://blog.csdn.net/xht555/article/details/38685845 https://www.cnblogs.com/wuchao/archive/2012/12/ ...

  2. 用DataGridView导入TXT文件,并导出为XLS文件

    使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据.也可以导出.txt,.xls等格式的文件.今天我们就先介绍一下用DataGridView把导入txt文件,导出x ...

  3. 从DataGridView导出Excel

    从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数). using System; using System.Collections.Generic; ...

  4. LINQ使用细节之.AsEnumerable()和.ToList()的区别

    先看看下面的代码,用了 .AsEnumerable(): 1 var query = (from a in db.Table2 where a = SomeCondition3 select a.So ...

  5. .NET组件控件实例编程系列——5.DataGridView数值列和日期列

    在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...

  6. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  7. WinForm DataGridView分页功能

    WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件  .CS: 1 using System; ...

  8. 如何在在WinFrom的DataGridView中做到数据持续动态加载而不卡死

    1.在这个过程我用过好几种办法 (1)使用委托的办法,这个方法可以做到持续加载,但是效果不理想会卡死 (2)开启线程的方法,会造成卡死 (3)使用另一个窗体的线程做持续加载(子窗体),让子窗体作为一个 ...

  9. WinForm中DataGridView显示更新数据--人性版

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

随机推荐

  1. .net的内置对象

    一 . 获取客户端,服务器端信息: Response.Write("客户端信息:"); Response.Write("<br>浏览器类型,版本:" ...

  2. python学习——urlparse模块

    urlparse模块: 1.urlparse() 具体程序及结果如下: >>> url = 'http://i.cnblogs.com/EditPosts.aspx?opt=1'&g ...

  3. xcode - 显示安装过的低版本模拟器

    1. 更改版本

  4. ADF 入门帮助

    本文是由英文帮助翻译所得: 1>task flows “任务流 task flows”可以包括非可视化的组件,比如方法调用.“页片段 page fragment”可以运行在一个页面的某个局部区域 ...

  5. Linux indent命令

    一.简介 indent可辨识C的原始代码文件,并加以格式化,以方便程序设计师阅读. 二.选项 http://www.cnblogs.com/xuxm2007/archive/2011/11/03/22 ...

  6. sklearn 算法大全

    http://scikit-learn.org/stable/tutorial/machine_learning_map/

  7. Git使用1

    1.先配置本地Git E:\personal>git config –-global user.name "lewy" E:\personal>git config – ...

  8. 抓包之网络分析器- Wiresshark

    https://www.wireshark.org/ Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包资料.Wi ...

  9. NSNotificationCenter 注意

    成对出现 意思很简单,NSNotificationCenter消息的接受线程是基于发送消息的线程的.也就是同步的,因此,有时候,你发送的消息可能不在主线程,而大家都知道操作UI必须在主线程,不然会出现 ...

  10. for() 和$.each()的用法区别

    一.对于数组 var arr=['姚明','易建联','张继科']; $.each(arr,function(index,value){ document.write(index+"=&qu ...