none
How can I do something on Row Header Click Event in DataGrid?

    Question

  • I want to do something when user click on row header of datagrid.

    for example: Assume that each row containing Customers details. When user clicks on particular row header, then another window should be opened containing orders from that particular customer.

    Please note that I want only this thing on Row Header's click or double click event. But there is no RowHeader click event in WPF. Is it possible?

    Friday, April 06, 2012 7:57 AM

Answers

  • Try using a combination of Style and EventSetter.

     <Style TargetType="{x:Type DataGridRow}">
                        <EventSetter Event="DataGridRowHeader.Click" Handler="Set_Ldiary_RowHeaderStyle"/>
                        <EventSetter Event="DataGridRow.Unselected" Handler="Unset_Ldiary_RowHeaderStyle"/>
                    </Style>

    For example, in the Handler above, instead of

    Handler="Set_Ldiary_RowHeaderStyle"

    use the MouseDoubleClick event that you have already created.

    Here's the complete sample code that you can play with:

    <Window x:Class="LdiaryDataGridRowHeaderClick.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Ldiary Translations" Height="350" Width="525">
        <Window.Resources>
            <Style x:Key="SelectedRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Content" Value="Selected"/>
                <Setter Property="Background" Value="Aquamarine"/>
            </Style>
            <Style x:Key="UnselectedRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Content" Value="RowHeader"/>
            </Style>
        </Window.Resources>
        <Grid>
            <DataGrid Name="dataGrid"
                      AutoGenerateColumns="False"
                      ItemsSource="{Binding}"
                      RowHeaderStyle="{StaticResource UnselectedRowHeaderStyle}">
                <DataGrid.ItemContainerStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <EventSetter Event="DataGridRowHeader.Click" Handler="Set_Ldiary_RowHeaderStyle"/>
                        <EventSetter Event="DataGridRow.Unselected" Handler="Unset_Ldiary_RowHeaderStyle"/>
                    </Style>
                </DataGrid.ItemContainerStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="FirstName" Binding="{Binding Path=FirstName}"/>
                    <DataGridTextColumn Header="LastName" Binding="{Binding Path=LastName}" Width="*"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace LdiaryDataGridRowHeaderClick
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            ObservableCollection<Person> people;
            public MainWindow()
            {
                InitializeComponent();
                people = new ObservableCollection<Person>(){
                    new Person{FirstName="Ldiary", LastName="Translation"},
                    new Person{FirstName="English", LastName="Japanese"}
                };
                dataGrid.DataContext = people;
            }
            private void Set_Ldiary_RowHeaderStyle(object sender, RoutedEventArgs e)
            {
                DataGridRow selectedRow = sender as DataGridRow;
                selectedRow.HeaderStyle = selectedRow.TryFindResource("SelectedRowHeaderStyle") as Style;
            }
            private void Unset_Ldiary_RowHeaderStyle(object sender, RoutedEventArgs e)
            {
                DataGridRow selectedRow = sender as DataGridRow;
                selectedRow.HeaderStyle = selectedRow.TryFindResource("UnselectedRowHeaderStyle") as Style;
            }
        }
        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    }



    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)

    • Marked as answer by Rana76 Friday, April 06, 2012 1:20 PM
    Friday, April 06, 2012 11:24 AM

All replies

  • Hi Rana

    I m little bit confused..... but what i understand , you should achieve this by using DataGridBeginingEdit event also..... 


    Harshad..... Always 4 U

    Friday, April 06, 2012 8:46 AM
  • Dear harjohn, Thanx for reply.

    How can be achieved this by using DataGridBeginingEdit event? 

    Sorry for my bad English. I try to explain by an example that what I want.

    A datagrid contains only two columns:

    1) CustomerID

    2) CustomerName

    Suppose the grid contains 10 rows with CustomerID from 1 to 10 and with Different Customer names.

    Now I want:

    when user clicks on any cell of CustomerID column, nothing should be happened.

    when user clicks on any cell of CusomerName column, nothing should be happened.

    But when user clicks on row header of any row, then a MessageBox should be displayed with CustomerID of that particular row.

    Please note that I have already achieved this task by coding behind MouseDoubleClick event and working properly. But this message box displays when user double clicks on any cell of datagrid or even double clicks row header. But I need, when user double clicks on row header only, not any other cell.

    Friday, April 06, 2012 9:37 AM
  • Hi Rana

    DataGird is having selected Item property, from which u can fetch the CustomerID, I mean as you select the row by click on row Header, that row will be selected and then You can fetch the cell value as You want.

    Friday, April 06, 2012 9:51 AM
  • Hi Rana

    As i said I m little bit confused..... I m not understand your problem that time .... now its clear..

    There is no such DataGridRowHeader Event in WPF DataGrid

    you done that by Mouse click event ... is good...

    another approach is like below

    http://forums.silverlight.net/t/159957.aspx/1

    Hope it will helps you .... :)


    Harshad..... Always 4 U

    Friday, April 06, 2012 9:51 AM
  • Try using a combination of Style and EventSetter.

     <Style TargetType="{x:Type DataGridRow}">
                        <EventSetter Event="DataGridRowHeader.Click" Handler="Set_Ldiary_RowHeaderStyle"/>
                        <EventSetter Event="DataGridRow.Unselected" Handler="Unset_Ldiary_RowHeaderStyle"/>
                    </Style>

    For example, in the Handler above, instead of

    Handler="Set_Ldiary_RowHeaderStyle"

    use the MouseDoubleClick event that you have already created.

    Here's the complete sample code that you can play with:

    <Window x:Class="LdiaryDataGridRowHeaderClick.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Ldiary Translations" Height="350" Width="525">
        <Window.Resources>
            <Style x:Key="SelectedRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Content" Value="Selected"/>
                <Setter Property="Background" Value="Aquamarine"/>
            </Style>
            <Style x:Key="UnselectedRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Content" Value="RowHeader"/>
            </Style>
        </Window.Resources>
        <Grid>
            <DataGrid Name="dataGrid"
                      AutoGenerateColumns="False"
                      ItemsSource="{Binding}"
                      RowHeaderStyle="{StaticResource UnselectedRowHeaderStyle}">
                <DataGrid.ItemContainerStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <EventSetter Event="DataGridRowHeader.Click" Handler="Set_Ldiary_RowHeaderStyle"/>
                        <EventSetter Event="DataGridRow.Unselected" Handler="Unset_Ldiary_RowHeaderStyle"/>
                    </Style>
                </DataGrid.ItemContainerStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="FirstName" Binding="{Binding Path=FirstName}"/>
                    <DataGridTextColumn Header="LastName" Binding="{Binding Path=LastName}" Width="*"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace LdiaryDataGridRowHeaderClick
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            ObservableCollection<Person> people;
            public MainWindow()
            {
                InitializeComponent();
                people = new ObservableCollection<Person>(){
                    new Person{FirstName="Ldiary", LastName="Translation"},
                    new Person{FirstName="English", LastName="Japanese"}
                };
                dataGrid.DataContext = people;
            }
            private void Set_Ldiary_RowHeaderStyle(object sender, RoutedEventArgs e)
            {
                DataGridRow selectedRow = sender as DataGridRow;
                selectedRow.HeaderStyle = selectedRow.TryFindResource("SelectedRowHeaderStyle") as Style;
            }
            private void Unset_Ldiary_RowHeaderStyle(object sender, RoutedEventArgs e)
            {
                DataGridRow selectedRow = sender as DataGridRow;
                selectedRow.HeaderStyle = selectedRow.TryFindResource("UnselectedRowHeaderStyle") as Style;
            }
        }
        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    }



    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)

    • Marked as answer by Rana76 Friday, April 06, 2012 1:20 PM
    Friday, April 06, 2012 11:24 AM
  • You can also modify the handler like this which is closer to what you want to achieve.

            private void Set_Ldiary_RowHeaderStyle(object sender, RoutedEventArgs e)
            {
                DataGridRow selectedRow = sender as DataGridRow;
                Person selectedPerson = (Person)dataGrid.Items.GetItemAt(selectedRow.GetIndex());
                MessageBox.Show(selectedPerson.FirstName);
            }


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)

    Friday, April 06, 2012 12:51 PM
  • Thanks Ldiary. 
    Friday, April 06, 2012 1:20 PM