Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

    Hey, Just trying to loop through my gridview and change the color of the row if the first cell equals a given value. This is the code I am trying but does not work.

    vb Code:
    1. With gvItemList
    2.             For i = 1 To .Rows.Count - 1
    3.                 If .Rows(i).Cells.Item(0).ToString = stValue Then
    4.                     .Rows(i).BackColor = Drawing.Color.CadetBlue
    5.                 End If
    6.             Next
    7.         End With

    thanks in advanced!

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Highlight row in gridview depending on a cell's value

    Quote Originally Posted by Besoup
    Hey, Just trying to loop through my gridview and change the color of the row if the first cell equals a given value. This is the code I am trying but does not work.

    vb Code:
    1. With gvItemList
    2.             For i = 1 To .Rows.Count - 1
    3.                 If .Rows(i).Cells.Item(0).ToString = stValue Then
    4.                     .Rows(i).BackColor = Drawing.Color.CadetBlue
    5.                 End If
    6.             Next
    7.         End With

    thanks in advanced!
    Hi,

    I think you can try something like this:

    Code:
    Dim row as DataGridView
    Dim genderCell as DataGridViewCell
    Dim nameCell as DataGridViewCell
    
    for each row in DataGridView1
        genderCell = row.Cells[2] 'gender column
        nameCell = row.Cells[0] 'name cell
        if CType(gender.Cell.Value, String) = "M" then
            nameCell.Style.BackColor = Color.Blue
        else
            nameCell.Style.BackColor = Color.Pink
        end if
    next
    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

    Thanks for the effort... got it with this code:

    vb Code:
    1. Dim i As Integer
    2.         With gvItemList
    3.             For i = 0 To .Rows.Count - 1
    4.                 If .Rows(i).Cells(0).Text = stValue Then
    5.                     .Rows(i).BackColor = Drawing.Color.CadetBlue
    6.                 End If
    7.             Next

  4. #4
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Iowa
    Posts
    298

    Re: [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

    Quote Originally Posted by Besoup View Post
    Thanks for the effort... got it with this code:

    vb Code:
    1. Dim i As Integer
    2.         With gvItemList
    3.             For i = 0 To .Rows.Count - 1
    4.                 If .Rows(i).Cells(0).Text = stValue Then
    5.                     .Rows(i).BackColor = Drawing.Color.CadetBlue
    6.                 End If
    7.             Next
    Can anyone tell me what gvItemList might be?
    Tuber

    "I don't know the rules"

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

    that is a GridView.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Iowa
    Posts
    298

    Re: [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

    Well using this code:

    vb Code:
    1. Dim stValue As String
    2.         Dim i As Integer
    3.  
    4.         With DataGridView1
    5.  
    6.             For i = 0 To .Rows.Count - 1
    7.  
    8.                 If .Rows(i).Cells(0).Text = stValue Then
    9.  
    10.                     .Rows(i).BackColor = Drawing.Color.CadetBlue
    11.  
    12.                 End If
    13.  
    14.             Next
    15.  
    16.         End With

    I'm getting


    Error 111 'BackColor' is not a member of 'System.Windows.Forms.DataGridViewRow'.

    AND

    Error 110 'Text' is not a member of 'System.Windows.Forms.DataGridViewCell'.
    Tuber

    "I don't know the rules"

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

    looks like you're using vs2003... DataGridView is the earlier version of the Gridview control which is probably why those properties aren't being recognized.

    I don't have alot of experience with the older version maybe someone else could help you in this post or you could try making a new one.

  8. #8
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Iowa
    Posts
    298

    Re: [RESOLVED] [2005] Highlight row in gridview depending on a cell's value

    Hm..I'm using 2005, maybe I just have the wrong thing :S
    Tuber

    "I don't know the rules"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width