Disable button when selecting item in DataGrid

0

I have a DataGrid with records and an Edit button. At the moment I'm doing the following: the user selects the item from the DataGrid and clicks the Edit button, to then be checked whether or not it can edit the record. I would like to disable the button once the item was selected in the DataGrid, without having to click the button to then be warned that it can not edit.

private void btnEditar_Click(object sender, RoutedEventArgs e)
    {
        if (dataGrid.SelectedItem != null)
        {
            if (Presenter.PodeEditar())
            {
                chamaMetodoEdicao();
            }
            else
                MessageBox.Show("Impossível editar!");
        }
        else
            MessageBox.Show("Selecione um item.");
    }
    
asked by anonymous 23.09.2015 / 19:13

1 answer

1

In Windows Forms you can use the CellEnter() ". This event is triggered whenever you click a cell (when the cell receives focus).

Example:

private void dataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    if (Presenter.PodeEditar())
       chamaMetodoEdicao();        
    else
        MessageBox.Show("Impossível editar!");
}

In WPF , you can use the SelectionChanged() ". Like% Windows Forms%, CellEnter() is fired every time a cell is clicked.

Example:

private void DataGrid_SelectionChanged(object sender, EventArgs e)
{
    if (Presenter.PodeEditar())
        chamaMetodoEdicao();        
    else
        MessageBox.Show("Impossível editar!");
}
    
23.09.2015 / 19:28