2012-12-19 4 views
1

당신은 내가 BindingSource에와 바인더 제본 된 DataGridView에서 발견 텍스트의 색상을 변경하려면 나에게 도움이 될, 내가 많이 검색하지만 대답은 여기에 을 찾을 수 couldnt는 한 내 코드데이터 소스를 바인딩 필터링 텍스트의 색상을 변경

입니다
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; 
    } 

답변

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){} 
} 
+0

변경하면 모든 행의 색이 바뀌므로 셀의 검색된 텍스트 만 변경하면됩니다. 검색된 텍스트는 검색 텍스트 상자에서 찾을 수 있도록 잘린 텍스트입니다. – Morteza

+0

내가 그것을 확인하게한다, 나는 그것을 즉시 편집 할 것이다. –

+0

@Morteza 내 대답을 확인하고, 내가 말한대로 편집했습니다. –

0

는 그리드의 RowDataBound 이벤트를 가질 필요가 그냥 코드를 호출 내부 마흐디 tahsildari

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); 
     } 
    } 
관련 문제