This is a migrated thread and some comments may be shown as answers.

GridVeiw Header selected event

2 Answers 51 Views
GridView
This is a migrated thread and some comments may be shown as answers.
imee
Top achievements
Rank 1
imee asked on 19 Nov 2009, 03:54 PM
hi,
i want to perform some action when ever user selects the row header of the grid view,
now problem is that i don't know what is the event name for Telerik GridView.

please, tell me the event name of row header selection by mouse.
thanks

2 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 19 Nov 2009, 04:34 PM
Hello imee,

When a row header is clicked the grid will perform a sort if the specified column can be sorted and that will fire the Sorting event. You can use this event to determine when a click action was performed.

Another approach is to manually subscribe for MouseDown/Up events:

public MainPage()
{
    InitializeComponent();
  
    this.myGrid.RowLoaded += new EventHandler<RowLoadedEventArgs>(myGrid_RowLoaded);
      
    // unfiortunately we have to turn off the horizontal virtualization to be able to subscribe for mouse event of a random header cell
    this.myGrid.EnableColumnVirtualization = false;
}
  
void myGrid_RowLoaded(object sender, RowLoadedEventArgs e)
{
    var headerRow = e.Row as GridViewHeaderRow;
  
    // if the header row is loaded
    if (headerRow != null)
    {
        headerRow.Cells[1].AddHandler(FrameworkElement.MouseLeftButtonDownEvent, 
            new MouseButtonEventHandler(MainPage_MouseLeftButtonDown), true);
    }
}

Yet another approach you can use is to provide a custom header cell template. This could come in handy if you also want to change the looks of a header cell:

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <Style x:Key="MyCustomHeaderCellTemplate" TargetType="gridView:GridViewHeaderCell">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="gridView:GridViewHeaderCell">
                        <Border Background="Transparent" 
                                MouseLeftButtonDown="Border_MouseLeftButtonDown">
                            <ContentPresenter Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <telerik:RadGridView x:Name="myGrid">
        <telerik:RadGridView.Columns>
            <telerik:GridViewColumn Header="CustomContent" 
                                    HeaderCellStyle="{StaticResource MyCustomHeaderCellTemplate}" >
            </telerik:GridViewColumn>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</Grid>

hope this helps.

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jorge Gonzalez
Top achievements
Rank 1
answered on 13 Jan 2010, 09:46 PM
Hello,
    Here's a slight variation of the second approach with the mouse events. This approach will cause the radGridViewVert_MouseLeftButtonDown event to only be executed when a header cell in the header column is clicked:

Note. My columns are not sortable.


private void radGridViewVert_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            var headerRow = e.Row as GridViewHeaderRow;

            if (e.Row is GridViewHeaderRow)
                if (headerRow != null)
                    headerRow.AddHandler(FrameworkElement.MouseLeftButtonDownEvent,
                         new MouseButtonEventHandler(radGridViewVert_MouseLeftButtonDown), true);
        }

        private void radGridViewVert_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ....
        }

Tags
GridView
Asked by
imee
Top achievements
Rank 1
Answers by
Milan
Telerik team
Jorge Gonzalez
Top achievements
Rank 1
Share this question
or