Gridview customised context menu

2 posts, 1 answers
  1. Sree
    Sree avatar
    30 posts
    Member since:
    Dec 2012

    Posted 05 Mar 2013 Link to this post

    Hello Team
    I have a requirement in gridview where user inserts columns to the grid and also removes when ever he wants, when he removes the column i need to delete the column from database, see the below image



    when user clicks the contextmenu Remove Field i need to delete the column from the database table

    for this i have written  MenuItemRemove.Click += new EventHandler(MenuItemRemove_Click);

    void MenuItemRemove_Click(object sender, EventArgs e)
    {
      string iColName = radgridview1.CurrentColumn.Name;
    // delete query to delete the above column from the database
    }

    my problem here is i selected fifth column in the grid, and the current column is always returning column index as 0 which means column one, how to capture the selected column index or name when clicked the context menu remove in my example

    please help me


  2. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 08 Mar 2013 Link to this post

    Hi Lakshmi,

    Thank you for writing.

    You can save the column in the menu item Tag, and then get a reference to it in the Click event handler. Here is an example:
    void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        RadMenuItem MenuItemRemove = new RadMenuItem("Remove column");
        e.ContextMenu.Items.Add(MenuItemRemove);
        MenuItemRemove.Tag = e.ContextMenuProvider;
        MenuItemRemove.Click += new EventHandler(MenuItemRemove_Click);
    }
     
    void MenuItemRemove_Click(object sender, EventArgs e)
    {
        RadMenuItem clickedItem = (RadMenuItem)sender;
        GridCellElement cellElement = (GridCellElement)clickedItem.Tag;
        GridViewColumn column = cellElement.ColumnInfo;
        string iColName = column.Name;
        RadMessageBox.Show(iColName);
        // delete query to delete the above column from the database
    }

    I hope this helps.
     

    Kind regards,
    Stefan
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top