2016-08-05 33 views
0

Ich möchte den angegebenen Suchtext in der DataGridView markieren. Ich habe cellFormatting-Ereignis versucht, um die Grenzen der searchtext zu finden und FillRectangle zu zeichnen, aber ich konnte nicht genau die Grenzen des Suchtextes erhalten.Wie markiert man Suchtext in DataGridView?

enter image description here

Im hinzugefügt Bild, ich habe versucht, Text „o“ zu markieren, aber es zeigt auch andere Zeichen.

Könnte mir jemand mitteilen, wie man perfektes Rechteck zeichnet, um den gesuchten Text hervorzuheben.

Grüße, Amal Raj.

+0

poste deinen Code, was du bis jetzt versucht hast. Siehe http://stackoverflow.com/questions/222322675/how-to-highlight-search-results-in-gridview-using-asp-net –

+0

Ich frage nach WinForms-Techniken. – Amal

+0

Sie müssen bestimmte Bedingung ausführen, wenn Sie Daten in Grid-Methode binden, können Sie etwas davon finden (http://stackoverflow.com/questions/10090691/add-progress-bar-in-gridview-using-datatable-oder- Dataset-in-window-application/10100992 # 10100992) –

Antwort

2

Sie müssen das CellPainiting-Ereignis verwenden. Versuchen Sie diesen Code:

string keyValue = "Co"; //search text 

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
    { 
     if (e.Value == null) return; 

     StringFormat sf = StringFormat.GenericTypographic; 
     sf.FormatFlags = sf.FormatFlags | StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DisplayFormatControl; 
     e.PaintBackground(e.CellBounds, true); 

     SolidBrush br = new SolidBrush(Color.White); 
     if (((int)e.State & (int)DataGridViewElementStates.Selected) == 0) 
      br.Color = Color.Black; 

     string text = e.Value.ToString(); 
     SizeF textSize = e.Graphics.MeasureString(text, Font, e.CellBounds.Width, sf); 

     int keyPos = text.IndexOf(keyValue, StringComparison.OrdinalIgnoreCase); 
     if (keyPos >= 0) 
     { 
      SizeF textMetricSize = new SizeF(0, 0); 
      if (keyPos >= 1) 
      { 
       string textMetric = text.Substring(0, keyPos); 
       textMetricSize = e.Graphics.MeasureString(textMetric, Font, e.CellBounds.Width, sf); 
      } 

      SizeF keySize = e.Graphics.MeasureString(text.Substring(keyPos, keyValue.Length), Font, e.CellBounds.Width, sf); 
      float left = e.CellBounds.Left + (keyPos <= 0 ? 0 : textMetricSize.Width) + 2; 
      RectangleF keyRect = new RectangleF(left, e.CellBounds.Top + 1, keySize.Width, e.CellBounds.Height - 2); 

      var fillBrush = new SolidBrush(Color.Yellow); 
      e.Graphics.FillRectangle(fillBrush, keyRect); 
      fillBrush.Dispose(); 
     } 
     e.Graphics.DrawString(text, Font, br, new PointF(e.CellBounds.Left + 2, e.CellBounds.Top + (e.CellBounds.Height - textSize.Height)/2), sf); 
     e.Handled = true; 

     br.Dispose(); 
    } 
+0

Lösung funktioniert gut. Aber wenn wir WrapMode eingestellt haben, deaktiviert die obige Formatierung den Umbruchmodus. Und Hervorhebung funktioniert nicht in umgebrochenen Modi. – Amal

+0

das funktioniert, aber für mich ist die Schriftart/Layout/Ausrichtung der Zellen völlig anders als in anderen Zellen, also muss ich mehr in das schauen. –