2

I'm trying to change an item on my RadContextMenu depending on the currently selected row in my RadGridView (edit: OrderList). I want the item to be enabled if the databound item in the current row has the correct property value.

The problem is that when I directly rightclick a row to open the RadContextmenu the CurrentRow has not yet been updated, so DropDownOpened is called with the old row. If I left click or double right click it works fine.

Here's a bit of the code:

OrderMenu.DropDownOpened += OrderMenu_DropDownOpened;

And the method

private void OrderMenu_DropDownOpened(object sender, EventArgs e)
{
    GoToParentOrderBtn.Enabled = GetSelectedOrder()?.ParentOrderId != null;
}

private OrderViewModel GetSelectedOrder()
{
    return (OrderViewModel)OrderList.CurrentRow.DataBoundItem;
}
Larzix
  • 55
  • 6

2 Answers2

0

Use dataGridView.EndEdit(); This function commits and ends the edit operation on the current cell being edited.

More info here

Abhishek
  • 2,925
  • 4
  • 34
  • 59
0

Sorry for not specifying that I'm using a radgridview.

I found a related answer which helped me solve my problem. I ended up making an extension (so I can use it all around the application) to RadGridView which fire an event on mousedown:

public partial class RadExtendedGridViewController : RadGridView
{
    public RadExtendedGridViewController()
    {
        InitializeComponent();
        base.MouseDown += RadExtendedGridViewController_MouseDown;
    }

    private void RadExtendedGridViewController_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            var element = this.ElementTree.GetElementAtPoint(e.Location);
            GridDataCellElement cell = element as GridDataCellElement;
            if (cell?.RowElement is GridDataRowElement)
            {
                Rows[cell.RowIndex].IsSelected = true;
            }
        }
    }
}

I then changed my GetSelectedOrder to using SelectedRows instead of Current:

    private OrderViewModel GetSelectedOrder()
    {
        return (OrderViewModel)OrderList.SelectedRows.FirstOrDefault()?.DataBoundItem;
    }

And now it works as intended. Thanks for taking your time trying to help me :-)

Larzix
  • 55
  • 6