Problem for adding context menu on header

5 posts, 1 answers
  1. Thomas LEBRUN
    Thomas LEBRUN avatar
    66 posts
    Member since:
    Sep 2009

    Posted 01 Sep 2009 Link to this post

    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 !

  2. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 04 Sep 2009 Link to this post

    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.
  3. DevCraft banner
  4. Thomas LEBRUN
    Thomas LEBRUN avatar
    66 posts
    Member since:
    Sep 2009
  5. William Schultz
    William Schultz avatar
    1 posts
    Member since:
    Nov 2009

    Posted 01 Apr 2010 Link to this post

    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?
  6. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 06 Apr 2010 Link to this post

    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.
Back to Top
DevCraft banner