更改绑定数据源中已过滤文本的颜色

时间:2012-12-19 07:53:52

标签: c# winforms datagridview colors find

你能帮我吗? 我想在绑定源绑定的datagridview中更改找到的文本的颜色, 我搜索了很多,但找不到答案 这是我的代码

private void Search()
    {
        var c = new[] {' '};
        var parts = searchTxt.Text.Split(c, StringSplitOptions.RemoveEmptyEntries);
        var condition = string.Empty;
        foreach (var t in parts)
        {
            if (condition.Trim().Length > 0) condition += " and ";
            condition += (string.Format(" (Family LIKE '*{0}*' or Email LIKE '*{0}*'" +
                                        " or Phone1 LIKE '*{0}*' or Phone2 LIKE '*{0}*'      or Phone3 LIKE '*{0}*' or Fax LIKE '*{0}*'" +
                                        " or CellPhone LIKE '*{0}*' or TypeofWork LIKE '*{0}*' or Address LIKE '*{0}*' or Brands LIKE '*{0}*') ",
                                        t));
        }
        ((BindingSource) dataGridView1.DataSource).Filter = condition;
    }

3 个答案:

答案 0 :(得分:1)

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    try
    {
        if (row.Cells[6].Value.ToString().Contains(textBox1.Text))
            row.Cells[6].Style.ForeColor = Color.Red;
    }
    catch (Exception){}
}

答案 1 :(得分:0)

要实现这一点,你需要为Grid提供RowDataBound事件,并且只需调用mahdi tahsildari引用的代码

答案 2 :(得分:0)

这里SW是(搜索字符串)

       private void DgNewItem_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

       {

      int strt = 0;

      int cnt = -1;

      int idx = -1;

        if ((((e.RowIndex >= 0) && (e.ColumnIndex >= 0))))

       {

         e.Handled = true;

    e.PaintBackground(e.CellBounds, true);


             **//string sw = GetSearchWord(e.ColumnIndex);**

            if (!string.IsNullOrEmpty(sw))
            {
                string val = e.FormattedValue.ToString();

                int sindx = val.ToLower().IndexOf(sw.ToLower());
                if ((sindx >= 0))
                {
                    while (strt != -1)
                    {
                        strt = val.ToLower().IndexOf(sw.ToLower(), idx + 1);
                        cnt += 1;
                        idx = strt;

                        //int numberOfTrues = Regex.Matches(val, sw).Count;
                        // the highlite rectangle
                        if (strt != -1)
                        {
                            Rectangle hl_rect = new Rectangle();
                            hl_rect.Y = (e.CellBounds.Y + 2);
                            hl_rect.Height = (e.CellBounds.Height - 5);
                            // find the size of the text before the search word
                            // and the size of the search word
                            string sBefore = val.Substring(0, idx);
                            string sWord = val.Substring(idx, sw.Length);
                            Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, e.CellStyle.Font, e.CellBounds.Size);
                            Size s2 = TextRenderer.MeasureText(e.Graphics, sWord, e.CellStyle.Font, e.CellBounds.Size);
                            // adjust the widths to make the highlite more accurate
                            if ((s1.Width > 5))
                            {
                                hl_rect.X = (e.CellBounds.X
                                + (s1.Width - 5));
                                hl_rect.Width = (s2.Width - 6);
                            }
                            else
                            {
                                hl_rect.X = (e.CellBounds.X + 2);
                                hl_rect.Width = (s2.Width - 6);
                            }
                            // use darker highlight when the row is selected
                            SolidBrush hl_brush;
                            if (((e.State & DataGridViewElementStates.Selected)
                            != DataGridViewElementStates.None))
                            {
                                hl_brush = new SolidBrush(Color.DarkGoldenrod);
                            }
                            else
                            {
                                hl_brush = new SolidBrush(Color.Violet);
                            }
                            // paint the background behind the search word
                            e.Graphics.FillRectangle(hl_brush, hl_rect);
                            hl_brush.Dispose();
                        }
                    }
                }
            }
            // paint the content as usual
            e.PaintContent(e.CellBounds);
        }
    }