dcsimg
Results 1 to 3 of 3

Thread: Wpf DataGrid Cell Background Color

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2016
    Posts
    55

    Wpf DataGrid Cell Background Color

    Following codes are okey.

    xaml

    Code:
    <Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="Button1" Height="25" Width="100" Content="Fill Data" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <Button x:Name="Button2" Height="25" Width="100" Content="Focus to Cell" VerticalAlignment="Top" HorizontalAlignment="Center"/>
        <Button x:Name="Button3" Height="25" Width="100" Content="Color to Cell" VerticalAlignment="Top" HorizontalAlignment="Right"/>
        <DataGrid x:Name="DataGrid1" Width="500" Height="200" SelectionMode="Single" SelectionUnit="Cell">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Yellow"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
        </DataGrid>
    </Grid>
    </Window>
    vb.net

    Code:
    Class MainWindow 
    
    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    
        Dim DataSet1 As System.Data.DataSet = New System.Data.DataSet
        Dim DataTable1 As System.Data.DataTable = New System.Data.DataTable
    
        DataTable1.Columns.Add("Name")
        DataTable1.Columns.Add("Sur Name")
        DataTable1.Columns.Add("Country")
        DataTable1.Columns.Add("Gender")
    
        DataTable1.Rows.Add({"Donald", "Trump", "United States", "Male"})
        DataTable1.Rows.Add({"Angela", "Merkel", "Germany", "Female"})
        DataTable1.Rows.Add({"Theresa", "May", "England", "Female"})
    
        DataSet1.Tables.Add(DataTable1)
        DataGrid1.ItemsSource = DataSet1.Tables(0).DefaultView
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
        DataGrid1.Focus()
        DataGrid1.CurrentCell = New DataGridCellInfo(DataGrid1.Items(1), DataGrid1.Columns(2))
        DataGrid1.SelectedCells.Clear()
        DataGrid1.SelectedCells.Add(DataGrid1.CurrentCell)
    End Sub
    
    Private Sub Button3_Click(sender As Object, e As RoutedEventArgs) Handles Button3.Click
        Dim myDataGridRow As DataGridRow = CType(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.Items(1)), DataGridRow)
        Dim myDataGridCell As DataGridCell = CType(DataGrid1.Columns(2).GetCellContent(myDataGridRow).Parent, DataGridCell)
        myDataGridCell.Background = New SolidColorBrush(CType(ColorConverter.ConvertFromString("#ff0000"), Color))
    End Sub
    
    End Class
    Please run the above codes and click Button1, Button2 and Button3 one by one in order to see everything is okey.

    Please understand that red color is never being changed even you click a cell.

    My question is here;

    Some people say that following code is not confidential.

    So, do you know alternative of following code?

    Code:
        Dim myDataGridRow As DataGridRow = CType(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.Items(1)), DataGridRow)
        Dim myDataGridCell As DataGridCell = CType(DataGrid1.Columns(2).GetCellContent(myDataGridRow).Parent, DataGridCell)
        myDataGridCell.Background = New SolidColorBrush(CType(ColorConverter.ConvertFromString("#ff0000"), Color))
    Last edited by Kram Kramer; Oct 2nd, 2018 at 08:03 PM.

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

    Re: Wpf DataGrid Cell Background Color

    Some people say that following code is not confidential.
    What do you mean by not confidential in context?

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal |Programming | GitHub
    ___________________________________________________________________________________

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

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,214

    Re: Wpf DataGrid Cell Background Color

    Thread moved from the 'VB.Net' forum to the 'WPF' forum

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width