Retrieving value of cell on right click

5 posts, 0 answers
  1. Steven
    Steven avatar
    7 posts
    Member since:
    Oct 2015

    Posted 27 Oct 2015 Link to this post

    So I know how to return any value using the CurrentRow. Let's say a user has one row selected, and they right-click on a different row, I need to retrieve the value from that row. I've been looking at CurrentRowChanging & Changed as I suspect I need to capture the value there. I think my issue is the value being returned now is the value from the previously selected row and I'm not aware of the event trigger hierarchy.

     Any help? Thanks

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 28 Oct 2015 Link to this post

    Hi Steven,

    Thank you for writing.

    You can use the MouseClick or the CellClick event:
    void radGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        GridDataCellElement element = radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
        if (element != null)
        {
            string value = element.Value.ToString();
        }
    }
     
    void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
    {
        string value = e.Value.ToString();
    }

    Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Steven
    Steven avatar
    7 posts
    Member since:
    Oct 2015

    Posted 29 Oct 2015 in reply to Dimitar Link to this post

    I haven't had any luck with this and I may have failed in explaining myself better.

    We have a custom context menu, and when that context menu is rendered it is supposed to pick up a value from a particular column on the selected row. As it is right now, it is picking up the value previously selected when the user right clicks on a different row. I need to pick up the new value from a column on the new row, the row the user right-clicked on.

    It works if the user first left-clicks to select the row, then right-clicks. But if all they do is right click on another arbitrary row, it's picking up the previous value.

    Thanks

  5. Steven
    Steven avatar
    7 posts
    Member since:
    Oct 2015

    Posted 29 Oct 2015 in reply to Steven Link to this post

    Update: I've tried capturing the value I need in the CurrentRowChanged (past tense right?), but in debug mode I can visually see the event fire before the new row is in fact changed.

    Is the only reliable means to left click one, then right click?

  6. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 30 Oct 2015 Link to this post

    Hi Steven,

    Thank you for writing back.

    In this case, it would be better to use the ContextMenuOpening event to retrieve the cell value. The following snippet shows how you can get the value in the item Click event handler:
    private RadContextMenu contextMenu;
    object cellValue = null;
     
    private void RadForm1_Load(object sender, EventArgs e)
    {
        contextMenu = new RadContextMenu();
        RadMenuItem menuItem1 = new RadMenuItem("Item 1");
     
        menuItem1.Click += menuItem1_Click;
     
        contextMenu.Items.Add(menuItem1);
    }
    void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        e.ContextMenu = contextMenu.DropDown;
        if (e.ContextMenuProvider is GridDataCellElement)
        {
            cellValue = ((GridDataCellElement)e.ContextMenuProvider).Value;
        }
    }
     
    void menuItem1_Click(object sender, EventArgs e)
    {
        Console.WriteLine(cellValue.ToString());
    }

    I hope this helps.

    Regards,
    Dimitar
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready