How to set the row back ground of a data grid programmatically

Yanıt How to set the row back ground of a data grid programmatically

  • 10 Ağustos 2012 Cuma 18:01
     
      Kod İçerir

    Hi all,

     How I can set the selected row background of a datagrid in wpf. 

    Regards

    Iqbal


    itismeiqbal

Tüm Yanıtlar

  • 10 Ağustos 2012 Cuma 18:29
     
     

    <DataGrid.Resources>

      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0000"/>

    </DataGrid.Resources>

    Source:  http://stackoverflow.com/questions/1223280/how-can-i-set-the-color-of-a-selected-row-in-datagrid

  • 10 Ağustos 2012 Cuma 18:33
     
     

    if I want to make the color of a particular row ie the row with index 10 to red . how to do it 


    itismeiqbal

  • 10 Ağustos 2012 Cuma 19:12
     
     Yanıt Kod İçerir

    Hello,

    Try this.

      
        
      public MainWindow()
            {
    
                InitializeComponent();   
    
           myGrid.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
               
            }
    
            void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
            {
                var generator = sender as ItemContainerGenerator;
                if (generator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                    (generator.ContainerFromIndex(0) as DataGridRow).Background = new SolidColorBrush(Colors.Red);
            }

    Regards.

  • 10 Ağustos 2012 Cuma 20:32
     
     Yanıt Kod İçerir

    if I want to make the color of a particular row ie the row with index 10 to red . how to do it 


    itismeiqbal

    You could simplify it a bit if you're looking for a particular index...

    private void colorRow(object sender, System.Windows.RoutedEventArgs e) { var row = myDataGrid.ItemContainerGenerator.ContainerFromIndex(10) as DataGridRow; row.Background = new SolidColorBrush(Colors.Red);

    }

    ~Christine

    My Gallery

  • 12 Ağustos 2012 Pazar 04:05
     
     
    Thanks it worked fine 

    itismeiqbal