C#使用Linq对DataGridView进行模糊查找
针对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进行模糊查找的更多相关文章
- C# 用Linq查询DataGridView行中的数据是否包含(各种操作)
http://blog.csdn.net/xht555/article/details/38685845 https://www.cnblogs.com/wuchao/archive/2012/12/ ...
- 用DataGridView导入TXT文件,并导出为XLS文件
使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据.也可以导出.txt,.xls等格式的文件.今天我们就先介绍一下用DataGridView把导入txt文件,导出x ...
- 从DataGridView导出Excel
从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数). using System; using System.Collections.Generic; ...
- LINQ使用细节之.AsEnumerable()和.ToList()的区别
先看看下面的代码,用了 .AsEnumerable(): 1 var query = (from a in db.Table2 where a = SomeCondition3 select a.So ...
- .NET组件控件实例编程系列——5.DataGridView数值列和日期列
在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...
- C# DataGridView自定义分页控件
好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...
- WinForm DataGridView分页功能
WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件 .CS: 1 using System; ...
- 如何在在WinFrom的DataGridView中做到数据持续动态加载而不卡死
1.在这个过程我用过好几种办法 (1)使用委托的办法,这个方法可以做到持续加载,但是效果不理想会卡死 (2)开启线程的方法,会造成卡死 (3)使用另一个窗体的线程做持续加载(子窗体),让子窗体作为一个 ...
- WinForm中DataGridView显示更新数据--人性版
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
随机推荐
- Python3 enumerate() 函数
Python3 enumerate() 函数 Python3 内置函数 描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标 ...
- 使用百度网盘配置私有Git服务
GitHub上免费的版本只能开源代码库,有时候需要配置些私有的服务,不方便公开.现在免费的网盘的容量越来越大,可以用来做存储的服务,如果只使用网盘存储合并代码很不方便,所以使用网盘+git 配置私有仓 ...
- 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree
[抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...
- OC - runtime - 1
- OC 线程操作3 - NSOperation
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- 2-string相关函数
string真的很好用,希望通过逐步的学习逐渐掌握的string的用法: 1. append() -- 在字符串的末尾添加字符 2. find() -- 在字符串中查找字符串 4. insert() ...
- 表单数据转换成json格式数据
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 理解OAuth 2.0 (摘自阮一峰网络日志)
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...
- vue cli+axios踩坑记录+拦截器使用,代理跨域proxy(更新)
16319 1.首先axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点建议方式 在main.js中如下声明使用 import axios from 'ax ...
- 解决Axure发布分享预览的3个方法
公司的同事制作的一个产品原型,要发给我,我当时正在客户这里,电脑上并没有Axure,客户又催得急,感到一阵无奈.这次回来之后,经过一番摸索,发现还是有办法的.这里给大家分享一下Axure发布分享预览的 ...