Results 1 to 6 of 6

Thread: WPF DataGrid Help

  1. #1

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    WPF DataGrid Help

    Hi All

    I want to loop through a datagrid and change the row color if a cell has a value is greater then my alarm value

    Sum thing like this
    Code:
    Dim Alarm1 as string = "245.00"
    
    For Each band As Forms.DataGridViewBand In DG1.Items
                    If DG1.Items(band.Index).Cells("Down Stream").Value > Alarm1 Then
                        band.DefaultCellStyle.BackColor = System.Drawing.Color.Red
                    End If
                Next
    Last edited by Brian Henry; Jul 13th, 2014 at 12:17 PM.
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

  2. #2
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: WPF DataGrid Help

    You should loop through Rows, not Bands:

    Code:
    Dim Alarm1 as Double = 245.00
            For Each row As DataGridViewRow In DG1.Rows
                If row.Cells("Down Stream").Value > Alarm1 Then
                    row.DefaultCellStyle.BackColor = Color.Red
                End If
            Next
    EDIT: Aso String is not the appropriate datatype to compare values. It is not a value. You know, string is just a string. It's not really a "number". If you have to use it as a string then you have to cast it to a Double in order to make it comparable with other values:
    Code:
    Dim Alarm1 as String = "245.00"
            For Each row As DataGridViewRow In DG1.Rows
                If row.Cells("Down Stream").Value > CDbl(Alarm1) Then
                    row.DefaultCellStyle.BackColor = Color.Red
                End If
            Next
    Last edited by Flashbond; Jul 11th, 2014 at 11:47 AM.
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: WPF DataGrid Help

    Moderator Action: Moved thread to WPF forums
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    Re: WPF DataGrid Help

    I'm getting an error on DG1.Rows ('Rows' is not a member of datagrid)
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

  5. #5
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: WPF DataGrid Help

    Aahh, becausw my code is for DataGridView. I think you should use bands then. I don't remember exactly the thing for DataGrid.
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: WPF DataGrid Help

    Using the extension method GetRow() here:
    How-to-set-DataGrid-Cell-Color.

    Here's how you implement it.
    vb.net Code:
    1. 'paint entire row code
    2. For index = 0 To grid1.Items.Count - 1 Step 1
    3.        Dim firstRow As DataGridRow = grid1.GetRow(index)
    4.        ' Cast to DataRowView, since my datagrid's itemsource property is of type DataTable.
    5.        ' Replace DirectCast if your ItemSource is a class.  
    6.        Dim item As DataRowView = DirectCast(firstRow.Item, DataRowView)
    7.        If item(0).ToString().Equals("245.00") Then
    8.               'set background
    9.               firstRow.Background = Brushes.Red
    10.        End If
    11. Next
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

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