找回密码
 立即注册

QQ登录

只需一步,快速开始

March

注册会员

5

主题

13

帖子

101

积分

注册会员

积分
101

活字格认证

最新发帖
March
注册会员   /  发表于:2014-9-18 11:13  /   查看:6784  /  回复:6
如图

一个单元格中的字符串颜色不同

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

6 个回复

倒序浏览
Alice
社区贡献组   /  发表于:2014-9-18 12:23:00
沙发
回复 1楼March的帖子

你好。图片上的效果C1FlexGrid可以实现。
设置C1FlexGrid的DrawMode为OwnerDraw,触发OwnerDrawCell事件。在这个事件里去使用不同的颜色和Font画文字就可以了。
请参考C1FlexGrid中文技术文档,地址:http://www.gcpowertools.com.cn/docs/ComponentOne/FlexGrid/
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
March
注册会员   /  发表于:2014-9-18 13:55:00
板凳
回复 2楼Alice的帖子

你好,我主要是想实现这样的效果,根据某个关键字查询FlecGrid中的数据,FlexGrid中匹配的字符串颜色变红变粗,或者加一个背景色,就像我在网页上查找关键字的效果一样,论坛上有没有相关示例,我是个FlexGrid新手:Z
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2014-9-18 14:32:00
地板
回复 3楼March的帖子

FlexGrid有FindRow方法,可以查找包含特定字符的某一行。
假设有一个TextBox和Button,在TextBox输入"Test"后,点击按钮要从第一列中查找是否有"Test"这个字符,忽略大小写,找到这个后,将字体颜色变红。
点击某个按钮开始查找的代码如下:
  1.   void _btnFind_Click(object sender, EventArgs e)
  2.         {
  3.             // use the regular FindRow
  4.             var index = this._flex.FindRow(_txtSearch.Text, 1, _flex.Col, false, true, true);
  5.             if (index > -1)
  6.             {
  7.                 this._flex.Row = index;
  8.                 C1.Win.C1FlexGrid.CellStyle cs = this._flex.Styles.Add("myStyle");
  9.                 cs.Font = new Font("Tahoma", 10, FontStyle.Bold);
  10.                 cs.ForeColor = Color.Red;
  11.                 this._flex.SetCellStyle(index, 1, cs);
  12.             }
  13.         }
复制代码


另外,对于新手,官方的中文文档和Sample都是不错的学习教材。
中文文档地址:http://www.gcpowertools.com.cn/docs/ComponentOne/FlexGrid/
Sample下载地址:http://www.gcpowertools.com.cn/p ... io_winform_demo.htm

提示:安装产品后也在开始菜单的ComponentOne下有相关Sample
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
March
注册会员   /  发表于:2014-9-18 15:18:00
5#
回复 4楼Alice的帖子

你这个方法是把整个单元格的字体颜色都改掉了吧,我是想找到后把单元格里匹配的文字字体改色
回复 使用道具 举报
March
注册会员   /  发表于:2014-9-18 16:17:00
6#
效果是大概实现了
代码
  1.         private void fgWork_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
  2.         {
  3.             if (e.Row >= fgWork.Rows.Fixed && fgWork.Cols[e.Col].Caption == "名称"&&e.Row==1)
  4.             {
  5.                 float x = e.Bounds.Left;
  6.                 float y = e.Bounds.Top;
  7.                 Graphics g = e.Graphics;
  8.                 string name1 = "阿加莎";
  9.                 g.DrawString(name1, new Font("宋体", 9), new SolidBrush(Color.Red), new PointF(x, y));
  10.                 Font Var_Font = new Font("宋体", 9);
  11.                 SizeF Var_Size = g.MeasureString(name1, Var_Font);
  12.                 x = x + Var_Size.Width;
  13.                 g.DrawString("水电费", new Font("宋体", 9), new SolidBrush(Color.Blue), new PointF(x, y));
  14.             }
  15.         }
复制代码




但是还有点问题,就是两种颜色的字体之间有点空隙

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2014-9-18 18:50:00
7#
回复 6楼March的帖子

看了你的代码,是你DrawString画的位置问题。
有关OwerDrawCell事件的用法,我这里有个代码片段和效果供你参考:
  1. private void c1FlexGrid1_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
  2. <pre>{
  3.     //Verify Row/Col index
  4.     if (e.Col == 1 &amp;&amp; e.Row == 1)
  5.     {
  6.       //Let the grid draw the BackGround and Border of the cell
  7.       e.DrawCell(C1.Win.C1FlexGrid.DrawCellFlags.Background | C1.Win.C1FlexGrid.DrawCellFlags.Border);

  8.       if (!(string.IsNullOrEmpty(e.Text)))
  9.       {
  10.         //Create an array of brushes and Fonts
  11.         SolidBrush[] brushes = new SolidBrush[] {
  12.                                                     new SolidBrush(Color.Red),
  13.                                                     new SolidBrush(Color.Green),
  14.                                                     new SolidBrush(Color.Blue),
  15.                                                     new SolidBrush(Color.Purple)
  16.                                                 };
  17.         Font[] fonts = new Font[] {
  18.                                         new Font("Verdana", 10),
  19.                                         new Font("Tahoma", 12,FontStyle.Underline),
  20.                                         new Font("Calibri", 8, FontStyle.Bold),
  21.                                         new Font("Calibri", 12, FontStyle.Italic)
  22.                                    };
  23.         Single x = 0;
  24.         string text = e.Text;
  25.         //Traverse celltext and set Font/ForeColor.
  26.         for (int i = 0; i < text.Length; i++)
  27.         {
  28.             string character = text<em class="bbcode-em"></em>.ToString();
  29.             e.Graphics.DrawString(character, fonts<em class="bbcode-em"></em>, brushes<em class="bbcode-em"></em>, e.Bounds.X + x, e.Bounds.Y);

  30.             //Next character needs to be shifted right;
  31.             //depending on the space occupied by the previous character as per its FontSize/FontStyle
  32.             x += (e.Graphics.MeasureString(character, this.c1FlexGrid1.Font)).Width;
  33.          }
  34.        }
  35.        //we're done
  36.        e.Handled = true;
  37.     }
  38. }
复制代码

效果:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部