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

ContextMenuStrip for child template

1 Answer 39 Views
GridView
This is a migrated thread and some comments may be shown as answers.
jules
Top achievements
Rank 1
jules asked on 08 Oct 2018, 06:23 AM

Hello, 

I am relatively new here, and would need help with how i can set different ContextMenusStrip based on selected child template in my RadGridView.

I am using one GridView with four child templates and would want each of them to be assigned a different ContextMenuStrip to ease  the operations around the data in each template. So my barrier now is now to programatically check which template is currently selected (focused) and bind the ContextMenuStrip accordingly or at least disable some buttons in it based on desired output

 

Thanks in advance for looking into this

Jules R

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Oct 2018, 11:08 AM
Hello, Jules, 

RadGridView offers the ContextMenuOpening event which is quite useful to specify what menu items to be displayed considering the target cell. In the ContextMenuOpeningEventArgs you have access to the target cell by the ContextMenuProvider. I have prepared a sample code snippet for your reference demonstrating how to display different menu items considering the child template. Additional information about the ContextMenuOpening event is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/context-menus/conditional-custom-context-menus

 



public RadForm1()
{
    InitializeComponent();
 
    DataTable dt1 = new DataTable();
    dt1.Columns.Add("Id", typeof(int));
    dt1.Columns.Add("Name", typeof(string));
    for (int i = 0; i < 5; i++)
    {
        dt1.Rows.Add(i, "Parent" + i);
    }
    this.radGridView1.MasterTemplate.DataSource = dt1;
    this.radGridView1.MasterTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
 
    Random rand = new Random();
    DataTable dt2 = new DataTable();
    dt2.Columns.Add("Id", typeof(int));
    dt2.Columns.Add("ParentId", typeof(int));
    dt2.Columns.Add("Title", typeof(string));
    for (int i = 6; i < 20; i++)
    {
        dt2.Rows.Add(i, rand.Next(0, 5), "Sub Item" + i);
    }
 
    GridViewTemplate childTemplate1 = new GridViewTemplate();
    childTemplate1.DataSource = dt2;
    childTemplate1.Caption = "First";
    childTemplate1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    radGridView1.MasterTemplate.Templates.Add(childTemplate1);
 
    GridViewRelation relation1 = new GridViewRelation(radGridView1.MasterTemplate);
    relation1.ChildTemplate = childTemplate1;
    relation1.RelationName = "Relation1";
    relation1.ParentColumnNames.Add("Id");
    relation1.ChildColumnNames.Add("ParentId");
    radGridView1.Relations.Add(relation1);
 
    DataTable dt3 = new DataTable();
    dt3.Columns.Add("Id", typeof(int));
    dt3.Columns.Add("ParentId", typeof(int));
    dt3.Columns.Add("Description", typeof(string));
    for (int i = 20; i < 40; i++)
    {
        dt3.Rows.Add(i, rand.Next(0, 5), "Sub Item" + i);
    }
 
    GridViewTemplate childTemplate2 = new GridViewTemplate();
    childTemplate2.DataSource = dt3;
    childTemplate2.Caption = "Second";
    childTemplate2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    radGridView1.MasterTemplate.Templates.Add(childTemplate2);
 
    GridViewRelation relation2 = new GridViewRelation(radGridView1.MasterTemplate);
    relation2.ChildTemplate = childTemplate2;
    relation2.RelationName = "Relation2";
    relation2.ParentColumnNames.Add("Id");
    relation2.ChildColumnNames.Add("ParentId");
    radGridView1.Relations.Add(relation2);
 
    this.radGridView1.ContextMenuOpening += radGridView1_ContextMenuOpening;
}
 
private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    GridDataCellElement targetCell = e.ContextMenuProvider as GridDataCellElement;
    if (targetCell != null && targetCell.RowInfo.HierarchyLevel == 1)//you are in the child level
    {
        e.ContextMenu.Items.Clear();
        if (targetCell.RowInfo.ViewTemplate.Caption == "First")
        {
            RadMenuItem item1 = new RadMenuItem("Item1");
            e.ContextMenu.Items.Add(item1);
        }
        else
        {
            RadMenuItem item2 = new RadMenuItem("Item2");
            e.ContextMenu.Items.Add(item2);
        }
    }
}

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
jules
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or