none
I want to highlight all the columns of a datagrid by mousehover of the column header just like incase of rows by default all the cells of a particular row is highlighted.

    Question

  • I want to highlight all the columns of a datagrid by mousehover of the column header just like incase of rows by default all the cells of a particular row is highlighted.Please help me regarding this.I need it urgent in my project.

    Tuesday, April 24, 2012 10:53 PM

Answers

  • ONce again i clear the problem, When ever you mouse over a datagrid all the cells of a row is by default highlighted when mouse is on the row.

    Could you share your code for me ?

    And you should to calculate which column you are over by yourself.

    Then use the code :

    Dispatcher.BeginInvoke(() =>
                {
                    foreach (var rowItem in dataGrid1.ItemsSource)
                    {
                        FrameworkElement el = dataGrid1.Columns[0].GetCellContent(rowItem);
                        (el.Parent as DataGridCell).Background = new SolidColorBrush(Colors.Red);
                    }
                });

    to set cells' background. You can target one single cell by:dataGrid1.Columns[0].GetCellContent(rowItem);.

    so you can do all what you want.

    Sunday, April 29, 2012 9:21 PM

All replies

  • Hi

    You can change the background of the datagrid cell in this way:

        
    <Grid x:Name="LayoutRoot" Background="White">
            <sdk:DataGrid x:Name="dataGrid1" 
                Height="140" Margin="0,5,0,10"
                AutoGenerateColumns="True" />
        </Grid>
       
     public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                dataGrid1.ItemsSource = Customer.GetSampleCustomerList();
                Dispatcher.BeginInvoke(() =>
                {
                    foreach (var rowItem in dataGrid1.ItemsSource)
                    {
                        FrameworkElement el = dataGrid1.Columns[0].GetCellContent(rowItem);
                        (el.Parent as DataGridCell).Background = new SolidColorBrush(Colors.Red);
                    }
                });
            }
        }
        public class Customer
        {
            public String FirstName { get; set; }
            public String LastName { get; set; }
            public String Address { get; set; }
            public Boolean IsNew { get; set; }
    
            // A null value for IsSubscribed can indicate 
            // "no preference" or "no response".
            public Boolean? IsSubscribed { get; set; }
    
            public Customer(String firstName, String lastName,
                String address, Boolean isNew, Boolean? isSubscribed)
            {
                this.FirstName = firstName;
                this.LastName = lastName;
                this.Address = address;
                this.IsNew = isNew;
                this.IsSubscribed = isSubscribed;
            }
    
            public static List<Customer> GetSampleCustomerList()
            {
                return new List<Customer>(new Customer[4] {
                    new Customer("A.", "Zero", 
                        "12 North Third Street, Apartment 45", 
                        false, true), 
                    new Customer("B.", "One", 
                        "34 West Fifth Street, Apartment 67", 
                        false, false),
                    new Customer("C.", "Two", 
                        "56 East Seventh Street, Apartment 89", 
                        true, null),
                    new Customer("D.", "Three", 
                        "78 South Ninth Street, Apartment 10", 
                        true, true)
                });
            }
        }

    Next,

    You can use the code below to get point where you move:

            
    private void dataGrid1_MouseMove(object sender, MouseEventArgs e)
            {
                try
                {
                    var max = (e.OriginalSource as TextBlock).TransformToVisual(dataGrid1);
                }
                catch (Exception){}
            }

    Calculate which column you are overing by the dataGrid1.Columns . change the cell color as you wish

    Hope helpful.

     

     

    Wednesday, April 25, 2012 11:36 PM
  • But my problem is not solved and also implementing above code there is an exception.ONce again i clear the problem, When ever you mouse over a datagrid all the cells of a row is by default highlighted when mouse is on the row.I need similar effect for Column in data grid.

    Friday, April 27, 2012 5:57 AM
  • ONce again i clear the problem, When ever you mouse over a datagrid all the cells of a row is by default highlighted when mouse is on the row.

    Could you share your code for me ?

    And you should to calculate which column you are over by yourself.

    Then use the code :

    Dispatcher.BeginInvoke(() =>
                {
                    foreach (var rowItem in dataGrid1.ItemsSource)
                    {
                        FrameworkElement el = dataGrid1.Columns[0].GetCellContent(rowItem);
                        (el.Parent as DataGridCell).Background = new SolidColorBrush(Colors.Red);
                    }
                });

    to set cells' background. You can target one single cell by:dataGrid1.Columns[0].GetCellContent(rowItem);.

    so you can do all what you want.

    Sunday, April 29, 2012 9:21 PM