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. 在此示例中,事件处理程序报告 RowErrorTextNeeded 事件的发生。In 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 或将消息附加到多行 TextBox中。To 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.

若要运行示例代码,请将其粘贴到包含名为 DataGridView1DataGridView 类型实例的项目中。To run the example code, paste it into a project that contains an instance of type DataGridView named DataGridView1. 然后,确保事件处理程序与 RowErrorTextNeeded 事件关联。Then 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


仅当设置 DataGridView 控件的 DataSource 属性或 trueVirtualMode 属性时,才会发生 RowErrorTextNeeded 事件。The 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 属性设置为 false,否则行标题中将显示错误标志符号。When 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.

只要检索 DataGridViewRow.ErrorText 属性的值,就会发生 RowErrorTextNeeded 事件。The 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 事件,以避免将行 ErrorText 值设置为多个行的性能损失。Handle 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.