DataGridView.RowHeightInfoNeeded 事件


请求有关行高度的信息时发生。Occurs when information about row height is requested.

 event System::Windows::Forms::DataGridViewRowHeightInfoNeededEventHandler ^ RowHeightInfoNeeded;
public event System.Windows.Forms.DataGridViewRowHeightInfoNeededEventHandler RowHeightInfoNeeded;
member this.RowHeightInfoNeeded : System.Windows.Forms.DataGridViewRowHeightInfoNeededEventHandler 
Public Custom Event RowHeightInfoNeeded As DataGridViewRowHeightInfoNeededEventHandler 


下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中,事件处理程序报告 RowHeightInfoNeeded 事件的发生。In the example, an event handler reports on the occurrence of the RowHeightInfoNeeded 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. 然后,确保事件处理程序与 RowHeightInfoNeeded 事件关联。Then ensure that the event handler is associated with the RowHeightInfoNeeded event.

private void DataGridView1_RowHeightInfoNeeded(Object sender, DataGridViewRowHeightInfoNeededEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "Height", e.Height );
messageBoxCS.AppendFormat("{0} = {1}", "MinimumHeight", e.MinimumHeight );
messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex );
MessageBox.Show(messageBoxCS.ToString(), "RowHeightInfoNeeded Event" );
Private Sub DataGridView1_RowHeightInfoNeeded(sender as Object, e as DataGridViewRowHeightInfoNeededEventArgs) _ 
     Handles DataGridView1.RowHeightInfoNeeded

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

End Sub


此事件可用于在执行排序操作后保留自定义行高度。This event is useful for preserving custom row heights after a sorting operation. 这是必需的,因为排序操作通常由数据源进行处理,而数据源并不跟踪数据行和控件中各行之间的对应关系。This is necessary because sorting operations are normally handled by the data source, which does not keep track of the correspondence between rows of data and rows in the control. 仅当设置了 DataSource 属性或 VirtualMode 属性 true时,才会发生此事件。This event occurs only when the DataSource property has been set or when the VirtualMode property is true. 在后一种情况下,你将提供自己的数据存储和排序操作。In the latter case, you provide your own data store and sorting operations. 处理 RowHeightInfoPushed 事件,以便在用户更改行高时存储更新的高度信息。Handle the RowHeightInfoPushed event to store updated height information when the user changes a row height. 当控件需要时,使用 RowHeightInfoNeeded 检索存储的高度信息。Use RowHeightInfoNeeded to retrieve the stored height information when the control needs it.

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