1

你能帮我吗?我想更改与 bindingsource 绑定的 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;
    }
4

3 回答 3

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){}
}
于 2012-12-19T08:06:26.530 回答
0

要实现这一点,您需要为 Grid 设置 RowDataBound 事件,并在内部调用 mahdi tahsildari 引用的代码

于 2012-12-19T08:12:54.717 回答
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);
        }
    }
于 2013-01-04T11:28:20.713 回答