DataGridViewCell.ErrorText 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定文字,描述與儲存格相關聯的錯誤狀況。
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
屬性值
描述與儲存格相關聯之錯誤狀況的文字。
- 屬性
範例
下列程式碼範例示範如何在處理未系結 DataGridView 中的錯誤狀況時使用這個屬性。 方法會將 AnnotateCell
錯誤訊息字串設定為 ErrorText 屬性。
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
備註
一般而言, ErrorText 處理 的 DataGridView 事件時 CellValidating 會使用 屬性。 如果儲存格的值失敗一些驗證準則,請將 的 屬性 DataGridViewCellValidatingEventArgs 設定 ErrorTextCancel 為 true
來取消認可作業。 然後,您指定的文字會由 DataGridView 顯示,並提示使用者修正資料格資料中的錯誤。
VirtualMode當 的 DataGridView 屬性為 true
時,您可以使用 和 CellErrorTextNeeded 事件,為數據列和儲存格 RowErrorTextNeeded 提供錯誤文字。
當您將不同的 ErrorText 字串指派給儲存格時, CellErrorTextChanged 會引發 控制項的事件 DataGridView 。
從 .NET Framework 4.5.2 開始,當app.config檔案包含下列專案時,系統 DPI 設定會決定錯誤圖示的大小調整:
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>