Muhammad Jamal Shaikh
Muhammad Jamal Shaikh

Reputation: 157

right click on grid row

problem is , that whenever the grid's row is right clicked the selected item is null.how do i make a the grid's row selected when any row was right clicked?

thanks Jamal

Upvotes: 0

Views: 717

Answers (2)

Gabe
Gabe

Reputation: 86718

I think the solution may have a problem. Every time a row is loaded it will add an event handler, so if the row is ever reused it can accumulate event handlers. I would recommend removing the event handler when the row is unloaded. Here's my suggested code:

private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown);
}
void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    dg.SelectedItem = ((sender) as DataGridRow).DataContext;
}
// new portion
private void dg_UnloadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown -= new MouseButtonEventHandler(Row_MouseRightButtonDown);
}

Upvotes: 1

Related Questions