DataGridView.RowErrorTextNeeded 事件

定義

發生於需要資料列的錯誤文字時。

public:
 event System::Windows::Forms::DataGridViewRowErrorTextNeededEventHandler ^ RowErrorTextNeeded;
public event System.Windows.Forms.DataGridViewRowErrorTextNeededEventHandler RowErrorTextNeeded;
public event System.Windows.Forms.DataGridViewRowErrorTextNeededEventHandler? RowErrorTextNeeded;
member this.RowErrorTextNeeded : System.Windows.Forms.DataGridViewRowErrorTextNeededEventHandler 
Public Custom Event RowErrorTextNeeded As DataGridViewRowErrorTextNeededEventHandler 

事件類型

範例

下列程式代碼範例示範如何使用這個成員。 在此範例中,事件處理程式會報告事件的發生次數 RowErrorTextNeeded 。 此報告可協助您瞭解事件發生的時間,並可協助您進行偵錯。 若要回報多個事件或經常發生的事件,請考慮將 取代 MessageBox.ShowConsole.WriteLine 或將訊息附加至多行 TextBox

若要執行範例程式代碼,請將它貼入包含名為 DataGridView1之類型DataGridView實例的專案。 然後,確定事件處理程式與 事件相關聯 RowErrorTextNeeded

private void DataGridView1_RowErrorTextNeeded(Object sender, DataGridViewRowErrorTextNeededEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "ErrorText", e.ErrorText );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "RowErrorTextNeeded Event" );
}
Private Sub DataGridView1_RowErrorTextNeeded(sender as Object, e as DataGridViewRowErrorTextNeededEventArgs) _ 
     Handles DataGridView1.RowErrorTextNeeded

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "ErrorText", e.ErrorText)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"RowErrorTextNeeded Event")

End Sub

備註

RowErrorTextNeeded只有在設定 控件的 DataGridView 屬性或其 VirtualMode 屬性為 trueDataSource,才會發生此事件。 RowErrorTextNeeded當您想要根據數據列的狀態及其包含的值來判斷數據列的錯誤時,處理事件很有用。

當您處理 RowErrorTextNeeded 事件並在處理程式中指定錯誤文字時,除非 ShowRowErrors 屬性設定為 false,否則數據列標頭中會出現錯誤字元。 當使用者將滑鼠指標移至錯誤字元上方時,錯誤文字會出現在工具提示中。

RowErrorTextNeeded每當擷取屬性的值DataGridViewRow.ErrorText時,也會發生此事件。

您可以使用 DataGridViewRowErrorTextNeededEventArgs.RowIndex 屬性來判斷數據列的狀態或其包含的值,並使用這項資訊來變更或修改 DataGridViewRowErrorTextNeededEventArgs.ErrorText 屬性。 這個屬性會使用事件值所覆寫的數據列 ErrorText 屬性值初始化。

RowErrorTextNeeded處理大量數據時,請處理 事件,以避免設定多個數據列的數據列ErrorText值效能負面影響。 如需詳細資訊,請參閱 縮放 Windows Form DataGridView 控制項的最佳做法

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱