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 exec 函数
Python3 exec 函数 Python3 内置函数 描述 exec 执行储存在字符串或文件中的 Python 语句,相比于 eval,exec可以执行更复杂的 Python 代码. 语法 以下 ...
- python连接redis,redis集群
python连接redis: import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900 ...
- 对象之int介绍
#Auther Bob #--*--conding:utf-8 --*-- #创建两个int的对象,age1和age2 age1 = 10 age2 = int(1) #查看对象的类 print(ty ...
- python的协程和异步io【select|poll|epoll】
协程又叫做微线程,协程是一种用户态的轻量级的线程,操作系统根本就不知道协程的存在,完全由用户来控制,协程拥有自己的的寄存器的上下文和栈,协程调度切换时,将寄存器上下文和栈保存到其他地方,在切换回来后, ...
- handler------post传送方式
package com.qianfeng.gp08_day26_hanlder2; import android.os.Bundle; import android.os.Handler; impor ...
- mvc EF 从数据库更新实体,添加视图实体时添加不上的问题
视图对象没有一列为非null的,解决办法,在视图中,将某一列排除为null的可能,比如:isnull(te,1),即可.
- JS 读取本地Excel文件
首先我们先引用一个Excel的类库xlsx.full.min.js 中间处理: 'use strict'; var ExcelReader = { isFirstRead: true, fixdata ...
- 深入了解 php 底层机制 (-)洪定坤
- noi 1997 最优乘车
H城是一个旅游胜地,每年都有成千上万的人前来观光.为方便游客,巴士公司在各个旅游景点及宾馆,饭店等地都设置了巴士站并开通了一些单程巴上线路.每条单程巴士线路从某个巴士站出发,依次途经若干个巴士站,最终 ...
- UI设计不就是画线框,凭什么年薪30W?
作为一枚界面设计师 我真的很想为UI设计抱不平啊!! UI设计真是一个备受不解的职业 常会被误解,然后出现以下场景 程序欧巴: 界面画好没?按钮圆的方的不都能用吗?纠结那多干嘛? 产品经理: 这次我们 ...