DataGridViewCell.ErrorText Propiedad
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene o establece el texto que describe una condición de error asociada a la celda.
public:
property System::String ^ ErrorText { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(false)]
public string ErrorText { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ErrorText : string with get, set
Public Property ErrorText As String
Valor de propiedad
Texto que describe una condición de error asociada a la celda.
- Atributos
Ejemplos
En el ejemplo de código siguiente se muestra cómo usar esta propiedad al controlar las condiciones de error en un elemento unbound DataGridView. El AnnotateCell
método establece una cadena de mensaje de error en la ErrorText propiedad .
void dataGridView1_CellValidating( Object^ /*sender*/, DataGridViewCellValidatingEventArgs^ newValue )
{
DataGridViewColumn^ column = dataGridView1->Columns[ newValue->ColumnIndex ];
if ( column->Name->Equals( "Track" ) )
{
CheckTrack( newValue );
}
else
if ( column->Name->Equals( "Release Date" ) )
{
CheckDate( newValue );
}
}
void CheckTrack( DataGridViewCellValidatingEventArgs^ newValue )
{
Int32 ignored;
if ( newValue->FormattedValue->ToString() == String::Empty )
{
NotifyUserAndForceRedo( "Please enter a track", newValue );
}
else
if ( !Int32::TryParse( newValue->FormattedValue->ToString(), ignored ) )
{
NotifyUserAndForceRedo( "A Track must be a number", newValue );
}
else
if ( Int32::Parse( newValue->FormattedValue->ToString() ) < 1 )
{
NotifyUserAndForceRedo( "Not a valid track", newValue );
editedLastColumn = true;
}
}
void NotifyUserAndForceRedo( String^ errorMessage, DataGridViewCellValidatingEventArgs^ newValue )
{
MessageBox::Show( errorMessage );
newValue->Cancel = true;
}
void CheckDate( DataGridViewCellValidatingEventArgs^ newValue )
{
try
{
DateTime::Parse( newValue->FormattedValue->ToString() ).ToLongDateString();
AnnotateCell( String::Empty, newValue );
}
catch ( FormatException^ /*ex*/ )
{
AnnotateCell( "You did not enter a valid date.", newValue );
}
}
void AnnotateCell( String^ errorMessage, DataGridViewCellValidatingEventArgs^ editEvent )
{
DataGridViewCell^ cell = dataGridView1->Rows[ editEvent->RowIndex ]->Cells[ editEvent->ColumnIndex ];
cell->ErrorText = errorMessage;
}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
if (column.Name == "Track")
{
CheckTrack(e);
}
else if (column.Name == "Release Date")
{
CheckDate(e);
}
}
private static void CheckTrack(DataGridViewCellValidatingEventArgs newValue)
{
Int32 ignored = new Int32();
if (String.IsNullOrEmpty(newValue.FormattedValue.ToString()))
{
NotifyUserAndForceRedo("Please enter a track", newValue);
}
else if (!Int32.TryParse(newValue.FormattedValue.ToString(), out ignored))
{
NotifyUserAndForceRedo("A Track must be a number", newValue);
}
else if (Int32.Parse(newValue.FormattedValue.ToString()) < 1)
{
NotifyUserAndForceRedo("Not a valid track", newValue);
}
}
private static void NotifyUserAndForceRedo(string errorMessage, DataGridViewCellValidatingEventArgs newValue)
{
MessageBox.Show(errorMessage);
newValue.Cancel = true;
}
private void CheckDate(DataGridViewCellValidatingEventArgs newValue)
{
try
{
DateTime.Parse(newValue.FormattedValue.ToString()).ToLongDateString();
AnnotateCell(String.Empty, newValue);
}
catch (FormatException)
{
AnnotateCell("You did not enter a valid date.", newValue);
}
}
private void AnnotateCell(string errorMessage, DataGridViewCellValidatingEventArgs editEvent)
{
DataGridViewCell cell = dataGridView1.Rows[editEvent.RowIndex].Cells[editEvent.ColumnIndex];
cell.ErrorText = errorMessage;
}
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
ByVal e As _
DataGridViewCellValidatingEventArgs) _
Handles dataGridView1.CellValidating
Dim column As DataGridViewColumn = _
dataGridView1.Columns(e.ColumnIndex)
If column.Name = "Track" Then
CheckTrack(e)
ElseIf column.Name = "Release Date" Then
CheckDate(e)
End If
End Sub
Private Shared Sub CheckTrack(ByVal newValue As DataGridViewCellValidatingEventArgs)
If String.IsNullOrEmpty(newValue.FormattedValue.ToString()) Then
NotifyUserAndForceRedo("Please enter a track", newValue)
ElseIf Not Integer.TryParse( _
newValue.FormattedValue.ToString(), New Integer()) Then
NotifyUserAndForceRedo("A Track must be a number", newValue)
ElseIf Integer.Parse(newValue.FormattedValue.ToString()) < 1 Then
NotifyUserAndForceRedo("Not a valid track", newValue)
End If
End Sub
Private Shared Sub NotifyUserAndForceRedo(ByVal errorMessage As String, ByVal newValue As DataGridViewCellValidatingEventArgs)
MessageBox.Show(errorMessage)
newValue.Cancel = True
End Sub
Private Sub CheckDate(ByVal newValue As DataGridViewCellValidatingEventArgs)
Try
DateTime.Parse(newValue.FormattedValue.ToString()).ToLongDateString()
AnnotateCell(String.Empty, newValue)
Catch ex As FormatException
AnnotateCell("You did not enter a valid date.", newValue)
End Try
End Sub
Private Sub AnnotateCell(ByVal errorMessage As String, _
ByVal editEvent As DataGridViewCellValidatingEventArgs)
Dim cell As DataGridViewCell = _
dataGridView1.Rows(editEvent.RowIndex).Cells( _
editEvent.ColumnIndex)
cell.ErrorText = errorMessage
End Sub
Comentarios
Normalmente, la ErrorText propiedad se usa al controlar el CellValidating evento de DataGridView. Si el valor de la celda produce un error en algunos criterios de validación, establezca la ErrorText propiedad y cancele la operación de confirmación estableciendo la Cancel propiedad de en DataGridViewCellValidatingEventArgstrue
. A continuación, se muestra el DataGridViewtexto que especifique y se le pide al usuario que corrija el error en los datos de la celda.
Cuando la VirtualMode propiedad de es true
, puede proporcionar texto de DataGridView error para las filas y celdas mediante los RowErrorTextNeeded eventos y CellErrorTextNeeded .
Cuando se asigna una cadena diferente ErrorText a una celda, se genera el CellErrorTextChanged evento del DataGridView control.
A partir de .NET Framework 4.5.2, el cambio de tamaño del icono de error viene determinado por la configuración de PPP del sistema cuando el archivo de app.config contiene la siguiente entrada:
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>