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

Problem for adding context menu on header

4 Answers 127 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas LEBRUN
Top achievements
Rank 1
Thomas LEBRUN asked on 01 Sep 2009, 04:07 PM
Hi,

I'm trying to add a context menu to the heder of my gridview. I've follow the code found here: http://demos.telerik.com/silverlight/#GridView/HeaderContextMenu

So I've used (I need to use only C# code,no XAML code):

DataGrid =

new SLGrid2();

 

 

GridViewHeaderMenu.SetIsEnabled(DataGrid.CurrentGrid as RadGridView, true);

 



DataGrid.CurrentGrid point to my RadGridView. The problem is that once te code is here:

IEnumerable

 

<GridViewHeaderCell> cells = row.Cells.OfType<GridViewHeaderCell>();

 

 

foreach (GridViewHeaderCell cell in cells)
{


row.Cells is always 0 so the foreach is never executed. My data are loaded after the creation of the grid so maybe it's because there is no data on the grid ? The problem is, if I tried to use the following line once the data are loaded:

GridViewHeaderMenu.SetIsEnabled(DataGrid.CurrentGrid as RadGridView, true);

The code use the Loaded event of the GridView, event that already occurs....

Do you have an idea ?

I hope it's clear :)


Thanks !

4 Answers, 1 is accepted

Sort by
0
Accepted
Pavel Pavlov
Telerik team
answered on 04 Sep 2009, 01:10 PM
Hi Thomas LEBRUN,

Here is some sample C# code that adds a context menu to the fisrt header cell of the RadGridView
public MainPage()  
        {  
            InitializeComponent();  
            this.RadGridView1.ItemsSource = Person.GetSampleListOfPersons();  
 
            this.RadGridView1.RowLoaded += new EventHandler<RowLoadedEventArgs>(RadGridView1_RowLoaded);  
        }  
 
        void RadGridView1_RowLoaded(object sender, RowLoadedEventArgs e)  
        {  
            GridViewHeaderRow headerRow = e.Row as GridViewHeaderRow;  
 
            if (headerRow != null)  
            {  
                GridViewHeaderCell firstHedaerCell = headerRow.Cells[0] as GridViewHeaderCell;  
                RadContextMenu contextMenu = new RadContextMenu();  
                contextMenu.Items.Add("test context menu item");  
                RadContextMenu.SetContextMenu(firstHedaerCell, contextMenu);  
            }  
        } 

Let me know if you have troubles  adadpting this to your project.

Best wishes,
Pavel Pavlov
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
Thomas LEBRUN
Top achievements
Rank 1
answered on 04 Sep 2009, 01:35 PM
0
William Schultz
Top achievements
Rank 1
answered on 01 Apr 2010, 10:50 PM
I have copied the same code you have above and set the grid view as :
        <TelerikControls:RadGridView  
            Name="GV1" 
            AutoGenerateColumns="True">  
        </TelerikControls:RadGridView> 
with your code behind:
    public partial class MainPage : UserControl {  
        public MainPage() {  
            InitializeComponent();  
            this.GV1.ItemsSource = InboxIssue.GetInboxIssues();  
 
            this.GV1.RowLoaded += new EventHandler<RowLoadedEventArgs>(GV1_RowLoaded);  
        }  
 
        protected void GV1_RowLoaded (Object sender, RowLoadedEventArgs e)  
        {  
            var headerRow = e.Row as GridViewHeaderRow;  
 
            if (headerRow != null) {  
                var firstHedaerCell = headerRow.Cells[0] as GridViewHeaderCell;  
                RadContextMenu contextMenu = new RadContextMenu();  
                contextMenu.Items.Add("test context menu item");  
                RadContextMenu.SetContextMenu(firstHedaerCell, contextMenu);  
            }     
        }  
    } 
and I get no context menu at all. In the RowLoaded even handler the header row has 1 cell but my data has 14 cols and once run it has a nice gridview with 1 data row and 14 columns with my data.

What am I doing wrong?
0
Vlad
Telerik team
answered on 06 Apr 2010, 07:14 AM
Hello,

It will be better if you set the context menu for the entire grid and use the approach demonstrated on this example:
http://demos.telerik.com/silverlight/#GridView/HeaderContextMenu

Best wishes,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
Thomas LEBRUN
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Thomas LEBRUN
Top achievements
Rank 1
William Schultz
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or