winform的DataGridView关键字高亮显示
来源:互联网 发布:保定优化公司 编辑:程序博客网 时间:2024/05/19 15:40
//自定义控件继承DataGridView
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace menu
{
public partial class MyDataGridView : DataGridView
{
public MyDataGridView()
{
InitializeComponent();
}
private string keyWord;
private Color keyColor = Color.Red;
public string KeyWord
{
get { return keyWord; }
set { keyWord = value; }
}
public Color KeyColor
{
get { return keyColor; }
set { keyColor = value; }
}
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
base.OnCellPainting(e);
if (e.Value != null && !string.IsNullOrEmpty(keyWord) && e.Value.ToString().Contains(keyWord))
{
string cellValue = e.Value.ToString();
Rectangle cellRect = e.CellBounds;
Rectangle keyRect = e.CellBounds;
float fontSizeWidth = 96 / (72 / e.CellStyle.Font.Size);
float fontSizeHeight = 96 / (72 / e.CellStyle.Font.Size);
string perKeyValue = cellValue.Substring(0, cellValue.IndexOf(keyWord));
int length = perKeyValue.Length;//关键字前的字符个数
int length2 = 2 * length - Encoding.Default.GetByteCount(perKeyValue);
if (length2 == 0)
keyRect.X += cellValue.Substring(0, cellValue.IndexOf(keyWord)).Length * (int)fontSizeWidth;
else
keyRect.X += cellValue.Substring(0, cellValue.IndexOf(keyWord)).Length * (int)fontSizeWidth - (length2 * (int)fontSizeWidth)/2;
keyRect.Y += (e.CellBounds.Height - (int)fontSizeHeight) / 2;
cellRect.Y = keyRect.Y;
Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor);
Brush keyBrush = new SolidBrush(this.keyColor);
e.PaintBackground(e.ClipBounds, false);
if (e.State == (DataGridViewElementStates.Displayed | DataGridViewElementStates.Selected | DataGridViewElementStates.Visible))
{
e.PaintBackground(e.ClipBounds, true);
}
e.Graphics.DrawString(cellValue, this.Font, foreBrush, cellRect, StringFormat.GenericDefault);
e.Graphics.DrawString(keyWord, this.Font, keyBrush, keyRect, StringFormat.GenericDefault);
e.Handled = true;
}
}
}
}
- winform的DataGridView关键字高亮显示
- DataGridView的高亮显示
- WinForm DataGridView 显示行号
- winform DataGridView 显示序号
- 高亮显示关键字
- 高亮显示关键字
- 高亮显示关键字
- 高亮显示关键字
- 关键字高亮显示
- 关键字高亮显示
- 关键字高亮显示
- 关键字高亮显示
- 关键字高亮显示
- 关键字高亮显示
- 与关键字的高亮显示相关
- jquery高亮显示搜索的关键字
- WinForm全屏显示DataGridView控件
- winform DataGridView 分页显示数据
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- 哈希查找因何快?我们使用它需要付出什么代价
- VC 打开文件框选择文件
- 在C++中使用TinyXML2解析xml
- 一些CSS学习网站
- winform的DataGridView关键字高亮显示
- 常用排序算法的时间复杂度和空间复杂度
- 套接字I/O模型之WSAEventSelect
- 最近 由于项目需要被迫自己写一个 ajax gird 记录-1
- cocos2d-x VA_LIST的用法
- MFC控件使用
- 关于finish的startActivity
- Java加密技术(六)——数字签名算法DSA
- 监控SQL:通过SQL Server的DDL触发器来监控数据库结构的变化(1)