Procedura: convalidare l'input con il controllo DataGrid Windows Form

Aggiornamento: novembre 2007

Nota:

Benché il controllo DataGridView sostituisca il controllo DataGrid precedente aggiungendo funzionalità, il controllo DataGrid viene mantenuto per compatibilità con le versioni precedenti e per utilizzo futuro se lo si desidera. Per ulteriori informazioni vedere Differenze tra i controlli DataGridView e DataGrid di Windows Form.

Sono disponibili due tipi di convalida dell'input per il controllo DataGrid Windows Form. Se l'utente tenta di immettere un valore relativo a un tipo di dati inaccettabile per la cella, ad esempio una stringa anziché un valore integer, il nuovo valore non valido verrà sostituito con il valore precedente. Questa convalida dell'input viene eseguita automaticamente e non può essere personalizzata.

L'altro tipo di convalida dell'input può essere utilizzato per rifiutare tutti i dati inaccettabili, ad esempio un valore 0 in un campo che deve essere maggiore o uguale a 1 oppure una stringa non corretta. Questa operazione viene eseguita nel dataset mediante la scrittura di un gestore eventi per l'evento ColumnChanging o RowChanging. Nell'esempio qui di seguito viene utilizzato l'evento ColumnChanging, poiché il valore non accettabile non viene ammesso specificamente per la colonna "Product". È possibile utilizzare l'evento RowChanging per verificare che il valore di una colonna "End Date" sia successivo a quello della colonna "Start Date", presente nella stessa riga.

Per convalidare l'input dell'utente

  1. Scrivere il codice per gestire l'evento ColumnChanging della tabella interessata. Quando viene rilevato un input non corretto, chiamare il metodo SetColumnError dell'oggetto DataRow.

    Private Sub Customers_ColumnChanging(ByVal sender As Object, _
    ByVal e As System.Data.DataColumnChangeEventArgs)
       ' Only check for errors in the Product column
       If (e.Column.ColumnName.Equals("Product")) Then
          ' Do not allow "Automobile" as a product.
          If CType(e.ProposedValue, String) = "Automobile" Then
             Dim badValue As Object = e.ProposedValue
             e.ProposedValue = "Bad Data"
             e.Row.RowError = "The Product column contians an error"
             e.Row.SetColumnError(e.Column, "Product cannot be " & _
             CType(badValue, String))
          End If
       End If
    End Sub
    
    //Handle column changing events on the Customers table
    private void Customers_ColumnChanging(object sender, System.Data.DataColumnChangeEventArgs e) {
    
       //Only check for errors in the Product column
       if (e.Column.ColumnName.Equals("Product")) {
    
          //Do not allow "Automobile" as a product
          if (e.ProposedValue.Equals("Automobile")) {
             object badValue = e.ProposedValue;
             e.ProposedValue = "Bad Data";
             e.Row.RowError = "The Product column contains an error";
             e.Row.SetColumnError(e.Column, "Product cannot be " + badValue);
          }
       }
    }
    
    //Handle column changing events on the Customers table
    private void Customers_ColumnChanging(System.Object sender, System.Data.DataColumnChangeEventArgs e) 
    {
       //Only check for errors in the Product column
       if ( e.get_Column().get_ColumnName().Equals("Product") ) 
       {
          //Do not allow "Automobile" as a product
          if ( e.get_ProposedValue().Equals("Automobile") ) 
          {
             System.Object badValue = e.get_ProposedValue();
             e.set_ProposedValue("Bad Data");
             e.get_Row().set_RowError("The Product column contains an error");
             e.get_Row().SetColumnError(e.get_Column(), "Product cannot be " + badValue);
          }
       }
    }
    
  2. Connettere il gestore eventi all'evento in questione.

    Inserire il codice qui di seguito nell'evento Load del form o nel relativo costruttore.

    ' Assumes the grid is bound to a dataset called customersDataSet1
    ' with a table called Customers.
    ' Put this code in the form's Load event or its constructor.
    AddHandler customersDataSet1.Tables("Customers").ColumnChanging, AddressOf Customers_ColumnChanging
    
    // Assumes the grid is bound to a dataset called customersDataSet1
    // with a table called Customers.
    // Put this code in the form's Load event or its constructor.
    customersDataSet1.Tables["Customers"].ColumnChanging += new DataColumnChangeEventHandler(this.Customers_ColumnChanging);
    
    // Assumes the grid is bound to a dataset called customersDataSet1
    // with a table called Customers
    // Put this code in the form's Load event or its constructor.
    customersDataSet1.get_Tables().get_Item("Customers").add_ColumnChanging(
       new DataColumnChangeEventHandler(this.Customers_ColumnChanging));
    

Vedere anche

Riferimenti

DataGrid

ColumnChanging

SetColumnError

Altre risorse

Controllo DataGrid (Windows Form)