Question How to highlight data in datagridview column cell

andimanro

Member
Joined
Jun 7, 2010
Messages
18
Programming Experience
1-3
dear all..

is it possible to highlight the data in datagridview column cell after we type some characters in datagridview filter bar ?
please give me a sample code how to do that...


Thnx in advance
 
read this article How to: Host Controls in Windows Forms DataGridView Cells

it may be more time consuming than what you want (isn't it always) but by creating your own column/cell objects you can customize behavior. you could most likely base your new class on the DataGridViewTextboxColumn / DataGridViewTextboxCell objects and just extend the functionality.

good luck!
 
Last edited by a moderator:
dear roccoman..
thnx for the reply...

can you give me a sample code how to solve my problem ? coz i'm still a newbie in vb net and i'm still didn't get how to implement the code on the link you have post...

thnx in advance
 
not at work atm but the jist is this:

- create your own column class & make it inherit from DataGridViewColumn
- create your own cell class & make it inherit from DataGridViewTextBoxCell


public Class MyCustomTextColumn
inherit DataGridViewColumn
end Class

public Class MyCustomTextCell
inherit DataGridViewTextBoxCell
end Class



this is the start; the example in the link is the exact same thing, only they are creating a CalendarCell object instead.

if the example is too much to understand then i would suggest not going down this road - it would be a big headache.
 
is it possible to highlight the data in datagridview column cell after we type some characters in datagridview filter bar ?
please give me a sample code how to do that...
If I understand you correctly, you have some kind of search/filter that display relevant rows, and now you want to highlight (BackColor perhaps?) some cells in the displayed rows?
From the sound of it this is what you need to work with: DataGridView.CellFormatting Event (System.Windows.Forms)
 
Dear John...

Sorry if my english is bad :) Let me explain again
Assume i have data in my table :
dog
horse
cat
bird

also assume i enter character "o" in the filter bar..... automatically the datagridview will show : dog and horse

now the problem is, how to highlight only the character "o" from the result (dog and horse) after filtering happens...

thnx
 
In that case you need to custom paint the cells. It is not awfully difficult, but it has some complexities. I will post a sample code that you can read and use, and reseach to learn more about.
VB.NET:
Private search As String

Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    If search = String.Empty OrElse e.ColumnIndex = -1 OrElse e.RowIndex = -1 Then Return
    Dim text As String = CStr(e.FormattedValue)
    Dim ix As Integer = text.IndexOf(search)
    If text = String.Empty OrElse ix = -1 Then Return
    Dim ranges As New List(Of CharacterRange)
    Do Until ix = -1
        ranges.Add(New CharacterRange(ix, search.Length))
        ix = text.IndexOf(search, ix + search.Length)
    Loop
    Dim strFormat As StringFormat = StringFormat.GenericTypographic
    strFormat.SetMeasurableCharacterRanges(ranges.ToArray)
    ' default alignment, translate e.CellStyle.Alignment if needed.
    strFormat.LineAlignment = StringAlignment.Center
    strFormat.Alignment = StringAlignment.Near
    'painting
    Dim selected As Boolean = (e.State And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected
    e.PaintBackground(e.ClipBounds, selected)
    For Each hilight As Region In e.Graphics.MeasureCharacterRanges(text, e.CellStyle.Font, e.CellBounds, strFormat)
        e.Graphics.FillRegion(Brushes.Yellow, hilight)
        hilight.Dispose()
    Next
    strFormat.Dispose()
    e.PaintContent(e.ClipBounds)
    e.Paint(e.ClipBounds, DataGridViewPaintParts.Border)
    e.Handled = True
End Sub
At some point the seach text is set, perhaps from a Button click, then call Refresh to have the grid repaint.
VB.NET:
Me.search = "test"
Me.DataGridView1.Refresh()
The code may be included in a custom DataGridView where you add the HilightString as a property.
 
Back
Top