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

Retrieving value of cell on right click

4 Answers 456 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 27 Oct 2015, 09:45 PM

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

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 28 Oct 2015, 10:45 AM
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
0
Steven
Top achievements
Rank 1
answered on 29 Oct 2015, 03:51 PM

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

0
Steven
Top achievements
Rank 1
answered on 29 Oct 2015, 04:35 PM

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?

0
Dimitar
Telerik team
answered on 30 Oct 2015, 07:40 AM
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
Tags
GridView
Asked by
Steven
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Steven
Top achievements
Rank 1
Share this question
or