Results 1 to 4 of 4

Thread: [RESOLVED] How to set DataGrid Cell Color

  1. #1

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

    [RESOLVED] How to set DataGrid Cell Color

    I'm trying to set the cell color of a WPF datagrid. But I can't get this to work.

    Code:
                    Dim firstRow As DataGridRow = TryCast(DG1.ItemContainerGenerator.ContainerFromItem(DG1.Items(0)), DataGridRow)
                    Dim firstColumnInFirstRow As DataGridCell = TryCast(DG1.Columns(0).GetCellContent(firstRow).Parent, DataGridCell)
                    'set background
                    firstColumnInFirstRow.Background = Brushes.Red
    Last edited by Brian Henry; Jul 14th, 2014 at 08:37 AM.
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

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

    Re: How to set DataGrid Cell Color

    Your code throws an exception. In order to access datagrid row/cell, define an extension method to return the DataGridRow.

    vb.net Code:
    1. Module DataGridExtensions
    2. <Extension()>
    3. Function GetRow(ByVal grid As DataGrid, ByVal index As Integer) As DataGridRow
    4. Dim row As DataGridRow = DirectCast(grid.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow)
    5. If row Is Nothing Then
    6. grid.UpdateLayout()
    7. grid.ScrollIntoView(grid.Items(index))
    8. row = DirectCast(grid.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow)
    9. End If
    10. Return row
    11. End Function
    12. End Module
    usage:
    vb.net Code:
    1. Dim firstRow As DataGridRow = grid1.GetRow(0)
    2. Dim firstColumnInFirstRow As Controls.DataGridCell = DirectCast(grid1.Columns(0).GetCellContent(firstRow).Parent, Controls.DataGridCell)
    3. 'set background
    4. firstColumnInFirstRow.Background = Brushes.Red

    KGC
    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...

  3. #3

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

    Re: How to set DataGrid Cell Color

    Thanks for your help it work now.
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

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

    Re: [RESOLVED] How to set DataGrid Cell Color

    Hi,

    Your welcome....

    KGC
    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