Disable button when selecting item in Datagrid

Asked

Viewed 234 times

0

I have a datagrid with records and an edit button. At the moment I am doing the following: the user selects the Datagrid item and clicks the Edit button, to then be performed the verification whether or not he can edit the record. I would like to disable the button as soon as the item was selected in Datagrid, without having to click the button to be warned that it cannot 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.");
    }
  • Do you want to disable the button when any row is selected? Or do you want to do some validation according to the contents of a specific cell? Post the code you made to the button to validate whether or not it can edit the record.

  • Exactly, I want to do the validation according to the contents of the selected cell. I will edit the question.

  • By the way, your project is Windows Forms or WPF?

  • My project is WPF

  • You have to use the tag WPF. I edited it for you. I’ll change the answer.

1 answer

1


In Windows Forms you can use the event CellEnter(). This event is triggered whenever you click on 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 the WPF, you can use the method SelectionChanged(). Just like the CellEnter() of Windows Forms the SelectionChanged() is fired whenever a cell is clicked.

Example:

private void DataGrid_SelectionChanged(object sender, EventArgs e)
{
    if (Presenter.PodeEditar())
        chamaMetodoEdicao();        
    else
        MessageBox.Show("Impossível editar!");
}
  • The problem is that I don’t have Datagridviewcelleventargs on WPF

  • @Mathi901 edited.

  • I had problems with the event Currentcellchanged, I was passing the value of the cell as null and giving Nullreferenceexception. I ended up using the Selectionchanged and it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.