https://blog.csdn.net/weixin_42974146/article/details/116355412
1.转载自:https://www.cnblogs.com/lgx5/p/7544681.html
针对DataGridView中已进行过数据绑定,即已向DataGridView中添加了一些数据,可以结合Linq查询,并让匹配查询的行高亮显示。
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;
<span class="token keyword">public</span> <span class="token function">frmWelcome</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token function">InitializeComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">frmWelcome_Load</span><span class="token punctuation">(</span><span class="token keyword">object</span> sender<span class="token punctuation">,</span> <span class="token class-name">EventArgs</span> e<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">dataGridViewInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/// <summary> </span>
<span class="token comment">/// DataGridView添加数据、初始化 </span>
<span class="token comment">/// </summary> </span>
<span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">dataGridViewInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
Dictionary<span class="token operator"><</span>String<span class="token punctuation">,</span> String<span class="token operator">></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token generic-method"><span class="token function">Dictionary</span><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"Lily"</span><span class="token punctuation">,</span> <span class="token string">"22"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"Andy"</span><span class="token punctuation">,</span> <span class="token string">"25"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
map<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span><span class="token string">"Peter"</span><span class="token punctuation">,</span> <span class="token string">"24"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 在这里必须创建一个BindIngSource对象,用该对象接收Dictionary<T, K>泛型集合的对象 </span>
<span class="token class-name">BindingSource</span> bindingSource <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BindingSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 将泛型集合对象的值赋给BindingSourc对象的数据源 </span>
bindingSource<span class="token punctuation">.</span>DataSource <span class="token operator">=</span> map<span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>dataGridView<span class="token punctuation">.</span>DataSource <span class="token operator">=</span> bindingSource<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">SearchButton_Click</span><span class="token punctuation">(</span><span class="token keyword">object</span> sender<span class="token punctuation">,</span> <span class="token class-name">EventArgs</span> e<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>KeyWord<span class="token punctuation">.</span>Text<span class="token punctuation">.</span><span class="token function">Equals</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// Linq模糊查询 </span>
IEnumerable<span class="token operator"><</span>DataGridViewRow<span class="token operator">></span> enumerableList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>dataGridView<span class="token punctuation">.</span>Rows<span class="token punctuation">.</span><span class="token generic-method"><span class="token function">Cast</span><span class="token punctuation"><</span><span class="token class-name">DataGridViewRow</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
List<span class="token operator"><</span>DataGridViewRow<span class="token operator">></span> list <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">from</span> item <span class="token keyword">in</span> enumerableList
<span class="token keyword">where</span> item<span class="token punctuation">.</span>Cells<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Value<span class="token punctuation">.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">IndexOf</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>KeyWord<span class="token punctuation">.</span>Text<span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">0</span>
<span class="token keyword">select</span> item<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ToList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 恢复之前行的背景颜色为默认的白色背景 </span>
<span class="token keyword">this</span><span class="token punctuation">.</span>dataGridView<span class="token punctuation">.</span>Rows<span class="token punctuation">[</span>beforeMatchedRowIndex<span class="token punctuation">]</span><span class="token punctuation">.</span>DefaultCellStyle<span class="token punctuation">.</span>BackColor <span class="token operator">=</span> System<span class="token punctuation">.</span>Drawing<span class="token punctuation">.</span>Color<span class="token punctuation">.</span>White<span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>list<span class="token punctuation">.</span>Count <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token comment">// 查找匹配行高亮显示 </span>
<span class="token keyword">int</span> matchedRowIndex <span class="token operator">=</span> list<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Index<span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>dataGridView<span class="token punctuation">.</span>Rows<span class="token punctuation">[</span>matchedRowIndex<span class="token punctuation">]</span><span class="token punctuation">.</span>DefaultCellStyle<span class="token punctuation">.</span>BackColor <span class="token operator">=</span> System<span class="token punctuation">.</span>Drawing<span class="token punctuation">.</span>Color<span class="token punctuation">.</span>Yellow<span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>beforeMatchedRowIndex <span class="token operator">=</span> matchedRowIndex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
2.转载自https://www.cnblogs.com/cqj98k/p/10573030.html
定义实体类:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
为实体类赋值存放到集合list中:
List<Student> lsStudent = new List<Student>(){
new Student { ID = 1,Name = "1", Age = 128, Sex = null },
new Student { ID = 2, Name = "2", Age = 18, Sex = "女" },
new Student { ID = 3, Name = "31", Age = 13, Sex = "男" },
new Student { ID = 4, Name = "4", Age = 23, Sex = "男" },
new Student { ID = 5, Name = "51", Age = null, Sex = "女" },
new Student { ID = 6, Name = "6", Age = null, Sex = "女" },
new Student { ID = 7, Name = "7", Age = null, Sex = "女" }
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
模糊查询条件:根据Name=1或者Sex=女模糊查询
string key = “1”;
string sex = “女”;
方法一:linq
List<Student> resultList = (from c in lsStudent where c.Name.Contains(key) ||c.Sex.Contains(sex) select c).ToList();
- 1
知识讲解:
- Contains(“key”),—意义等同于ql server中的like ‘%key%’,从两端模糊匹配
- StartsWith(“key”),—意义等同于sql server中的like ‘key%’,从开头模糊匹配
- EndsWith(“key”),—意义等同于sql server中的like ‘%key’,从结尾模糊匹配
方法二:
List<Student> resultList = lsStudent.Where(str => str.Name.Contains(key) || str.Sex.Contains(key)).ToList();
- 1
方法三:
List<Student> resultList = (from c in lsStudent where c.Name.IndexOf(key)>=0 ||c.Sex.IndexOf(sex)>=0 select c).ToList();
- 1
知识讲解:
- c.Name.IndexOf(key)>=0 —意义等同于 like ‘%key%’,从两端模糊匹配
- c.Name.StartsWith(key) —等同于like ‘key%’ ,从开头模糊匹配
- c.Name.EndWith(key) —等同于like ‘%key’,从结尾模糊匹配