注册 登录
编程论坛 C# 论坛

winfrom中datagridview显示数据库内容,将其中的关键字描红显示

tiejiang509 发布于 2012-02-28 15:48, 2131 次点击
我用vs里的datagridview显示数据的查询内容,内容以表格的形式显示在datagridview中,想把第二列中的关键字描红。只描红关键字,其他的字颜色不变。哪位大仙帮忙解决一下,找了好几天了,都没有找到解决方法。我刚注册就20分,全部给了
14 回复
#2
xydddaxia2012-02-29 10:26
程序代码:
//重绘单元格
    public class MyDataGridView : DataGridView
    {
        public MyDataGridView()
        {

        }

        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);
                keyRect.X += cellValue.Substring(0, cellValue.IndexOf(keyWord)).Length * (int)fontSizeWidth;
                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;
            }
        }
    }


//设置样式dataGridView1 为MyDataGridView类型
            this.dataGridView1.KeyColor = Color.Red;
            this.dataGridView1.KeyWord = "工程";



只有本站会员才能查看附件,请 登录




#3
tiejiang5092012-02-29 14:50
回复 2楼 xydddaxia
真是高手啊,我刚学C#,麻烦你能详细说一下吗?就是该怎么一步步的做呢,界面需要放个datagridview控件,然后怎么做呢。重绘单元格的方法要放到什么地方呢?麻烦解释一下,非常感谢你。
#4
xydddaxia2012-02-29 15:19
新建一个pulic 类 MyDataGridView 继承 DataGridView,新建的这个类会生成一个控件,生成后可以在工具箱找到,和原来的DataGridView一模一样,只是重写里面的OnCellPainting方法,并且添加了2个字段和属性
#5
tiejiang5092012-02-29 15:46
回复 4楼 xydddaxia
我还是看不懂,感觉有点深。您能抽点时间将详细步骤写一下吗?我急需这个方法。如建一个什么样的程序,添加一个什么样类型的文件,再在这个文件中怎么做,知道我用来显示数据内容的datagridview要怎么和重绘的单元格关联。在你有时间的情况下,麻烦给谢谢,非常感谢。
#6
tiejiang5092012-02-29 16:16
回复 4楼 xydddaxia
我QQ 710793123。您能加我吗,我是一个初学者
#7
zjdjh2012-03-02 21:27
以下是引用xydddaxia在2012-2-29 15:19:47的发言:

新建一个pulic 类 MyDataGridView 继承 DataGridView,新建的这个类会生成一个控件,生成后可以在工具箱找到,和原来的DataGridView一模一样,只是重写里面的OnCellPainting方法,并且添加了2个字段和属性
认真学习!!!
版主说的比书上说的好懂!!!!!
#8
hlm7509082014-06-20 00:55
这个超强
#9
幽你一默2014-07-01 08:21
请问怎么设置样式dataGridView1 为MyDataGridView类型呢?
#10
幽你一默2014-07-01 08:27
我懂了  谢谢   果然大牛!
#11
hlm7509082014-07-07 01:17
怎么实现多关键字 呢
关键字中间用 ; 隔开

分割后循环 肯定也行,不过会不会 效率很差呢
#12
hlm7509082014-07-11 14:09
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;

namespace Winform中DataGridView单元格内容字体突出显示
{
    class MyDataGridView : DataGridView
    {

        public MyDataGridView()
        {

        }
        public Color KeyColor { get; set; }
        public string KeyWord { get; set; }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            base.OnCellPainting(e);

            if (e.Value != null)
            {
                string cellValue = e.Value.ToString();//单元格原本内容
                Rectangle cellRect = e.CellBounds;//默认单元格
                float fontSizeWidth = 96 / (72 / e.CellStyle.Font.Size);
                float fontSizeHeight = 96 / (72 / e.CellStyle.Font.Size);
                //用一个list来保存关键字及对应的区域
                List<KeyValuePair<string, Rectangle>> cc = new List<KeyValuePair<string, Rectangle>>();

                //分解关键字;隔开的
                string[] al = this.KeyWord.Split(';');
                foreach (string i in al)
                {
                    if (cellValue.Contains(i))
                    {
                        Rectangle ee = e.CellBounds;  //单元格内容区域,默认定义为单元格大小
                        
//关键字的坐标
                        ee.X += cellValue.Substring(0, cellValue.IndexOf(i)).Length * (int)fontSizeWidth; //关键字左下角点坐标
                        ee.Y += (e.CellBounds.Height - (int)fontSizeHeight) / 2;//关键字右上角点坐标
                        cc.Add(new KeyValuePair<string, Rectangle>(i, ee));//收集到所有关键字的区域
                    }
                }
                if (cc.Count > 0)
                {
                    //原文本的Y坐标
                    cellRect.Y = cc[0].Value.Y;//这一句有bug,对自动换行的有问题

                    
//绘制背景色
                    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, new SolidBrush(e.CellStyle.ForeColor), cellRect, StringFormat.GenericDefault);

                    cc.ForEach(p =>
                    {
                        e.Graphics.DrawString(p.Key, this.Font, new SolidBrush(this.KeyColor), p.Value, StringFormat.GenericDefault);
                    });

                    //提交事务
                    e.Handled = true;
                }

            }

        }
    }
}
#13
hlm7509082014-07-11 14:11
只有本站会员才能查看附件,请 登录


[ 本帖最后由 hlm750908 于 2014-7-11 14:12 编辑 ]
#14
hlm7509082014-07-11 14:17
this.myDataGridView1.KeyWord = "试;本4";   //多关键字用  ; 分隔

但是这个不适用于自动换行
bug 在 坐标定位上
#15
hlm7509082014-07-11 17:38
高人可以帮忙 修正 一下吗
只有本站会员才能查看附件,请 登录
1