locked
Highlight Datagrid Cell based on a string value RRS feed

  • Question

  • I'm trying to get a value in a datagrid to highlight based on the value from a textbox, I can't seem to figure out why it's not working right.

     

            Dim xdata As String = TextBox1.Text
            Dim cell As DataGridViewCell
            Dim cellVar As String
            For Each row As DataGridViewRow In Me.Tbl_NameDataGridView.Rows
                cell = row.Cells(0)
                cellVar = row.Cells(0).Value
                If cellVar.Contains(xdata) Then
                    cell.Style.BackColor = Color.Red
                End If
            Next



    I will be legendary someday, just not today.
    Friday, October 7, 2011 4:41 PM

Answers

  • Here is how I got it to work:

    on the LOAD event of the form:

            Dim i As Integer
            For i = 0 To Tbl_NameDataGridView.Columns.Count - 1
                Tbl_NameDataGridView.Columns(i).Name = Tbl_NameDataGridView.Columns(i).DataPropertyName
            Next
            ' Don't allow new records to be added.
            Tbl_NameDataGridView.AllowUserToAddRows = False


    On the datagridview_cellformating event:

            If Me.Tbl_NameDataGridView.Columns(e.ColumnIndex).Name = "Name" Then
                If e.Value IsNot Nothing Then
                    Dim sVal As String = CType(e.Value, String)
                    sVal = sVal.ToLower
                    If ((sVal.IndexOf(TextBox1.Text) > -1)) Then
                        e.CellStyle.BackColor = Color.Pink
                    End If
                End If
            End If

    and then on the button_click event to activate it:

     Me.Tbl_NameDataGridView.Invalidate()



    I will be legendary someday, just not today.
    • Marked as answer by mreed72 Thursday, October 13, 2011 5:34 PM
    Thursday, October 13, 2011 5:33 PM

All replies

  • When is this code supposed to run (and change the cell formatting)?


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, October 7, 2011 5:08 PM
  • It's inside a ButtonClick event
    I will be legendary someday, just not today.
    Friday, October 7, 2011 5:10 PM
  • Hi

    Try this example in a new project with default form, splitcontainer and 1 button

    Public Class Form1
    
        ' new project with default form with 1 splitcontainer and 1 default button in panel2
    
        ' create a DataGridView
        Private DGV As New DataGridView
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            ' create 4 columns
            Dim Col1, Col2, Col3, Col4 As New DataGridViewTextBoxColumn
    
            ' add columns to grid
            DGV.Columns.AddRange(Col1, Col2, Col3, Col4)
            ' set last column to occupy remaining 'space' on grid
            DGV.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    
            ' set grid to occupy all of form
            DGV.Dock = DockStyle.Fill
    
            ' set column names
            Col1.Name = "ID"
            Col2.Name = "Param1"
            Col3.Name = "Param2"
            Col4.Name = "Other Data"
    
            ' add grid to form
            Me.SplitContainer1.Panel1.Controls.Add(DGV)
    
            ' create dummy test data and add rows to grid
            Dim r As New Random
            For i As Integer = 2 To 18
                DGV.Rows.Add(i, r.Next(9, 21), r.Next(88, 111), "Unknown data")
                Select Case i
                    Case 9, 11, 13
                        ' add misc rows for string matching example
                        DGV.Rows.Add(i, r.Next(9, 21), r.Next(88, 111), "Test Data")
                    Case 3, 5, 17
                        ' add misc rows for string matching example
                        DGV.Rows.Add(i, r.Next(9, 21), "Test Data2", "Unknown data")
                    Case 2, 14, 18
                        ' add misc rows for string matching example
                        DGV.Rows.Add(i, "Test Data3", r.Next(88, 111), "Unknown data")
    
                End Select
            Next
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each r As DataGridViewRow In dgv.rows
                With r
                    If Not .IsNewRow Then
                        If .Cells("Other Data").Value.Contains("Test Data") Then
                            .Cells(3).Style.BackColor = Color.Red
                            .Cells(3).Style.ForeColor = Color.Yellow
                        End If
    
                        If .Cells("Param1").Value.ToString.Contains("Test Data3") Then
                            .Cells(1).Style.BackColor = Color.Blue
                            .Cells(1).Style.ForeColor = Color.White
                        End If
    
                        If .Cells("Param2").Value.ToString.Contains("Test Data2") Then
                            .Cells(2).Style.BackColor = Color.Green
                            .Cells(2).Style.ForeColor = Color.White
                        End If
                    End If
                End With
            Next
        End Sub
    End Class
    
    


    Regards from Livingston, Scotland (Please ignore any stupid formatting produced by this forum)
    Friday, October 7, 2011 5:37 PM
  • Can you use the CellFormatting Event instead?

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

    You can call the Refresh method of the DataGridView to cause the event to fire for the cells.


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, October 7, 2011 5:38 PM
  • Hi,

    Try this following code

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            For Each roo As DataGridViewRow In DataGridView1.Rows
    
                For Each t As DataGridViewCell In roo.Cells
    
                    If t.Value = "Value to check" Then
                        t.Style.BackColor = Color.Red
                    End If
    
                Next
    
            Next
        End Sub
    

     

    Hope it helps,

    Regards,
    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Saturday, October 8, 2011 12:12 PM
  • Hi mreed72,

    You could also try below code which works fine.

        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Dim xdata As String = TextBox1.Text
            Dim cell As DataGridViewCell
            Dim cellVar As String
            Dim i As Integer
            For i = 0 To Me.DataGridView1.Rows.Count - 2
                cell = DataGridView1.Rows(i).Cells(1)
                cellVar = DataGridView1.Rows(i).Cells(1).Value.ToString
                If cellVar.Contains(xdata) Then
                    cell.Style.BackColor = Color.Red
                End If
            Next
        End Sub
    

    If you have any concerns, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 10, 2011 7:52 AM
  • Hi mreed72,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions? If you have any concerns, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 12, 2011 6:58 AM
  • Here is how I got it to work:

    on the LOAD event of the form:

            Dim i As Integer
            For i = 0 To Tbl_NameDataGridView.Columns.Count - 1
                Tbl_NameDataGridView.Columns(i).Name = Tbl_NameDataGridView.Columns(i).DataPropertyName
            Next
            ' Don't allow new records to be added.
            Tbl_NameDataGridView.AllowUserToAddRows = False


    On the datagridview_cellformating event:

            If Me.Tbl_NameDataGridView.Columns(e.ColumnIndex).Name = "Name" Then
                If e.Value IsNot Nothing Then
                    Dim sVal As String = CType(e.Value, String)
                    sVal = sVal.ToLower
                    If ((sVal.IndexOf(TextBox1.Text) > -1)) Then
                        e.CellStyle.BackColor = Color.Pink
                    End If
                End If
            End If

    and then on the button_click event to activate it:

     Me.Tbl_NameDataGridView.Invalidate()



    I will be legendary someday, just not today.
    • Marked as answer by mreed72 Thursday, October 13, 2011 5:34 PM
    Thursday, October 13, 2011 5:33 PM
  • I found your code very valuable, but it did process the last row on the grid.  Found that when I changed the -2 to a -1, then the code worked perfectly.

    Thanks again.


    ISV using VB.net and SQL Server Express

    Tuesday, May 29, 2018 9:05 PM