DataGridView.RowErrorTextNeeded 事件


在需要某一行的错误文本时发生。Occurs when a row's error text is needed.

 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 




下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中,事件处理程序报告事件的发生 RowErrorTextNeededIn the example, an event handler reports on the occurrence of the RowErrorTextNeeded event. 此报表可帮助你了解何时发生事件,并可帮助你进行调试。This report helps you to learn when the event occurs and can assist you in debugging. 若要报告多个事件或频繁发生的事件,请考虑 MessageBox.Show Console.WriteLine 将消息替换为多行或将其追加到多行 TextBoxTo report on multiple events or on events that occur frequently, consider replacing MessageBox.Show with Console.WriteLine or appending the message to a multiline TextBox.

若要运行示例代码,请将其粘贴到包含名为的类型的实例的项目中 DataGridView DataGridView1To run the example code, paste it into a project that contains an instance of type DataGridView named DataGridView1. 然后,确保事件处理程序与事件相关联 RowErrorTextNeededThen ensure that the event handler is associated with the RowErrorTextNeeded event.

private void DataGridView1_RowErrorTextNeeded(Object sender, DataGridViewRowErrorTextNeededEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "ErrorText", e.ErrorText );
messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex );
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.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex)
    MessageBox.Show(messageBoxVB.ToString(),"RowErrorTextNeeded Event")

End Sub


RowErrorTextNeeded仅当设置了控件的 DataSource 属性 DataGridView 或其 VirtualMode 属性为时,才会发生此事件 trueThe RowErrorTextNeeded event occurs only when the DataSource property of the DataGridView control is set or its VirtualMode property is true. RowErrorTextNeeded如果要根据行的状态和其包含的值确定行的错误,则处理事件会很有用。Handling the RowErrorTextNeeded event is useful when you want to determine the error for a row depending on its state and the values it contains.

处理 RowErrorTextNeeded 事件并在处理程序中指定错误文本时,除非将属性设置为,否则行标题中将显示错误标志符号 ShowRowErrors falseWhen you handle the RowErrorTextNeeded event and specify error text in the handler, an error glyph appears in the row header unless the ShowRowErrors property is set to false. 当用户将鼠标指针移到错误标志符号上方时,错误文本将显示在工具提示中。When the user moves the mouse pointer over the error glyph, the error text appears in a ToolTip.

RowErrorTextNeeded如果检索属性的值,也会发生该事件 DataGridViewRow.ErrorTextThe RowErrorTextNeeded event also occurs whenever the value of the DataGridViewRow.ErrorText property is retrieved.

您可以使用 DataGridViewRowErrorTextNeededEventArgs.RowIndex 属性来确定行的状态或它包含的值,并使用此信息来更改或修改 DataGridViewRowErrorTextNeededEventArgs.ErrorText 属性。You can use the DataGridViewRowErrorTextNeededEventArgs.RowIndex property to determine the state of a row or the values it contains, and use this information to change or modify the DataGridViewRowErrorTextNeededEventArgs.ErrorText property. 此属性将用行属性的值进行初始化,该属性的值将 ErrorText 重写。This property is initialized with the value of the row ErrorText property, which the event value overrides.

处理 RowErrorTextNeeded 大量数据时处理事件,以避免设置多行的行值时的性能损失 ErrorTextHandle the RowErrorTextNeeded event when working with large amounts of data to avoid the performance penalties of setting the row ErrorText value for multiple rows. 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

有关如何处理事件的详细信息,请参阅 处理和引发事件For more information about how to handle events, see Handling and Raising Events.