针对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. 36. Valid Sudoku (Array; HashTable)

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  2. python笔记之循环控制

    学习python的第一个例子,while循环中嵌套if-else语句,一个猜年龄的例子 #案例1,实现循环猜年龄 # my_age = 12 # while True: # guess_age1 = ...

  3. 算法笔记_067:蓝桥杯练习 算法训练 安慰奶牛(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是 ...

  4. RedisUtil工具类

    转载:http://blog.csdn.net/liuxiao723846/article/details/50401406 1.使用了jedis客户端,对redis进行了封装,包括: 1)使用了re ...

  5. Spring框架的事务管理之编程式的事务管理(了解)

    1. 说明:Spring为了简化事务管理的代码:提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可!!2.手动编程方式的具体步骤如下: 1.步骤 ...

  6. jq给动态生成的标签绑定事件的几种方法

    经常遇到给动态生成的标签绑定事件不好用,自己简单测试总结了下,结论如下了: body> <!-- 下面是用纯动态方式生成标签 --> <div id="d2" ...

  7. [SoapUI] 重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息

    重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息 package d ...

  8. 低配NOSQL

    东西写的太简单了 都不好意思说是NOSQL 其实就是STL 的map容器记录了写入的信息 解析了下数据仅此. 分析的时候想了很多 比如学习redis的自写hash,动态调整hash表容量. 比如右值或 ...

  9. android 混淆文件proguard.cfg详解 (转载)

    -injars  androidtest.jar[jar包所在地址] -outjars  out[输出地址] -libraryjars    'D:\android-sdk-windows\platf ...

  10. SharedPreferences的基本使用-----存,删,改,查

    1.创建一个SharedPreferences对象 SharedPreferences spf = context.getSharedPreferences("imageload" ...