none
How to check a gridview cell value with logic RRS feed

  • Question

  • Goal:  to loop through a GridView, and query a columns cell for a text value, and apply If Then logic, based on the result.

    I tried a few variations on this, but the If Then does not function.

    No errors, just no color change.

    I know I'm querying the correct cell reference, because I have  a listbox I'm populating with the value of:

    lb1.Items.Add(String.Concat(nameToCheck, " ", i))
    
    and this populates the listbox with all of my cell(1) values just fine.

    For i = 0 To GridView1.Rows.Count - 1
    
    nameToCheck = GridView1.Rows(i).Cells(1).Text      
    nameToCheck.Trim()
    nameToCheck.ToUpper()
    lb1.Items.Add(String.Concat(nameToCheck, " ",)) 
    If nameToCheck = "CELLVALUE" Then GridView1.Rows(i).Cells(7).BackColor = Drawing.Color.BlueViolet
    
    Next

    Friday, November 17, 2017 9:28 PM

All replies

  • Goal:  to loop through a GridView, and query a columns cell for a text value, and apply If Then logic, based on the result.

    I tried a few variations on this, but the If Then does not function.

    No errors, just no color change.

    I know I'm querying the correct cell reference, because I have  a listbox I'm populating with the value of:

    lb1.Items.Add(String.Concat(nameToCheck, " ", i))
    and this populates the listbox with all of my cell(1) values just fine.

    For i = 0 To GridView1.Rows.Count - 1
    
    nameToCheck = GridView1.Rows(i).Cells(1).Text      
    nameToCheck.Trim()
    nameToCheck.ToUpper()
    lb1.Items.Add(String.Concat(nameToCheck, " ",)) 
    If nameToCheck = "CELLVALUE" Then GridView1.Rows(i).Cells(7).BackColor = Drawing.Color.BlueViolet
    
    Next

    You might try to simplify it and if that doesn't work, put a breakpoint in but something like this (the code is off the cuff so it might not be quite right):

    For Each dgvr As DataGridViewRow In DataGridView1.Rows
        If dgvr.Cells(1).Value.ToString IsNot Nothing
            Stop
        End If
    Next

    Something like that.

    Try it and let's see please?


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Friday, November 17, 2017 9:40 PM ...typo in the code
    Friday, November 17, 2017 9:39 PM
  • Hi

    You state that there were 'no errors'.

    I find that strange, because with the same code I saw loads of errors, in the editor alone. Of course, with those errors, it can't compile, therefore, I also find it strange that you say you ran the code.

    BTW: if you are using a DataGrid rather than a DataGridView then ignore this post, otherwise, checkout the code below.

    ' seriously, you should use these two Options
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With DataGridView1
                For i As Integer = 0 To 9
                    .Rows.Add(Nothing, "CELLVALUE", Nothing, Nothing, Nothing, Nothing, Nothing, i.ToString)
                Next
            End With
            ' used Pink as BackGround colour so I 
            ' could see the cell contents
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                If Not i = DataGridView1.NewRowIndex Then
                    Dim nameToCheck As String = DataGridView1(1, i).Value.ToString.Trim.ToUpper
                    ListBox1.Items.Add(String.Concat(nameToCheck, " "))
                    If nameToCheck = "CELLVALUE" Then DataGridView1.Rows(i).Cells(7).Style.BackColor = Color.Pink
                End If
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, November 17, 2017 9:55 PM
    Friday, November 17, 2017 9:55 PM
  • Closest I've come is:

    For Each dgvr As GridViewRow In GridView1.Rows
    If dgvr.Cells(1).Text.ToString = "AVALUETOMATCH" Then dgvr.Cells(1).BackColor = Drawing.Color.Azure 
    End If
    Next

    but no luck: no cell color change

    btw, I'm using ASP/VB

    Maybe I'm trying to do something that's not possible, I dunno..

    I thought it would be easy to just turn a cell a color, or poke in some text, based on a cells value..

    Friday, November 17, 2017 9:58 PM

  • btw, I'm using ASP/VB

    ASP is a different animal; you're in the wrong forum.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, November 17, 2017 10:01 PM
  • If this is ASP.NET then use this forum https://forums.asp.net/

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, November 17, 2017 10:53 PM
    Moderator
  •  These two lines are not doing anything...

    nameToCheck.Trim()
    nameToCheck.ToUpper()
     

     If you want to trim the spaces from the ends of the nameToCheck string and convert it to upper case too,  then you need to assign the results of those lines to the (nameToCheck) string.  You can do both in a single line like below....

    nameToCheck = nameToCheck.Trim.ToUpper


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, November 17, 2017 11:21 PM Changed wording
    Friday, November 17, 2017 11:01 PM