针对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. TensorFlow—张量运算仿真神经网络的运行

    import tensorflow as tf import numpy as np ts_norm=tf.random_normal([]) with tf.Session() as sess: n ...

  2. 利用python实现二分法和斐波那契序列

    利用python实现二分法:我的实现思路如下 1.判断要查找的值是否大于最大值,如果大于则直接返回False 2.判断要查找的值是否小于最小值,如果小于则直接返回False 3.如果要查找的值在最大值 ...

  3. 两数之和-数据结构设计 · Two Sum - Data structure design

    [抄题]: 设计b并实现一个 TwoSum 类.他需要支持以下操作:add 和 find.add -把这个数添加到内部的数据结构.find -是否存在任意一对数字之和等于这个值 [思维问题]: 不知道 ...

  4. win8.1下cocos2d-x 3.x环境搭建

    Win8.1下Cocos2d-x 3.4环境搭建 第一步: 需要下载的:(Windows 64位系统下环境搭建) Ant   apache-ant-1.9.4-bin.zip NDK   androi ...

  5. windows 10 WSL 安装 Centos

    1. 打开 WSL,没啥好说的 使用管理员权限打开 powershell,执行 Enable-WindowsOptionalFeature -Online -FeatureName Microsoft ...

  6. 如何定义一个高逼格的原生JS插件

    插件的需求 我们写代码,并不是所有的业务或者逻辑代码都要抽出来复用.首先,我们得看一下是否需要将一部分经常重复的代码抽象出来,写到一个单独的文件中为以后再次使用.再看一下我们的业务逻辑是否可以为团队服 ...

  7. EXPAT(XML解析库)

    一.简介 expat是一个由C语言编写的XML解析库.James Clark创建了这个库,现在是制定XML标准的W3组织的技术leader.现在的版本是2.0.2.0开始就由Clark Cooper领 ...

  8. Strand Specific mRNA sequencing 之重要性与分析

    Strand Specific mRNA sequencing 之重要性与分析 发表评论 2,761 A+ 所属分类:Bioinformatics   收  藏 研究生物基因转录体的方法有许多种,而使 ...

  9. 大神真会玩~这组C4D动图,我都看了一整天!

    来自法国的Guillaume Kurkdjian 擅长创作一些平面动态图像 每张都诉说了一个小笑话或者小故事 像个极其微型的小电影. 这些动图的灵感可能来自某个交通工具 或是某个悠闲的时光 也可能是生 ...

  10. Laravel框架中实现supervisor执行异步进程

    问题描述:在使用Laravel框架实现动态网页时,若有些操作计算量较大,为了不影响用户体验,往往需要使用异步方式去处理.这里使用supervisor和laravel自带的queues实现. Super ...