1

バインディングソースでバインドされた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

ここで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 に答える
0

これを実装するには、Grid の RowDataBound イベントが必要で、内部で mahdi tahsildari によって参照されるコードを呼び出すだけです。

于 2012-12-19T08:12:54.717 に答える