DataGridView.CellErrorTextNeeded Event
Occurs when a cell's error text is needed.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
The CellErrorTextNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true. Handling the CellErrorTextNeeded event is useful when you want to determine the error for a cell depending on its value or state.
When you handle the CellErrorTextNeeded event and specify error text in the handler, an error glyph appears in the cell unless the ShowCellErrors property is set to false or the cell is in edit mode. When the user moves the mouse pointer over the error glyph, the error text appears in a ToolTip.
The CellErrorTextNeeded event also occurs whenever the value of the DataGridViewCell.ErrorText property is retrieved.
You can use the DataGridViewCellEventArgs.RowIndex and ColumnIndex property to determine the state or value of a cell, and use this information to change or modify the DataGridViewCellErrorTextNeededEventArgs.ErrorText property. This property is initialized with the value of the cell ErrorText property, which the event value overrides.
Handle the CellErrorTextNeeded event when working with large amounts of data to avoid the performance penalties of setting the cell ErrorText value for multiple cells. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.
For more information about handling events, see NIB: Consuming Events.
The following code example demonstrates how to create error messages for cells without unsharing rows or causing the cell error to be duplicated over all the shared cells.
Private WithEvents wholeTable As New ToolStripMenuItem() Private WithEvents lookUp As New ToolStripMenuItem() Private strip As ContextMenuStrip Private cellErrorText As String Private Sub dataGridView1_CellContextMenuStripNeeded(ByVal sender As Object, _ ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _ Handles dataGridView1.CellContextMenuStripNeeded cellErrorText = String.Empty If strip Is Nothing Then strip = New ContextMenuStrip() lookUp.Text = "Look Up" wholeTable.Text = "See Whole Table" strip.Items.Add(lookUp) strip.Items.Add(wholeTable) End If e.ContextMenuStrip = strip End Sub Private Sub wholeTable_Click(ByVal sender As Object, ByVal e As EventArgs) Handles wholeTable.Click dataGridView1.DataSource = Populate("Select * from employees", True) End Sub Private theCellImHoveringOver As DataGridViewCellEventArgs Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) _ Handles dataGridView1.CellMouseEnter theCellImHoveringOver = e End Sub Private cellErrorLocation As DataGridViewCellEventArgs Private Sub lookUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lookUp.Click Try dataGridView1.DataSource = Populate("Select * from employees where " & _ dataGridView1.Columns(theCellImHoveringOver.ColumnIndex).Name & " = '" & _ dataGridView1.Rows(theCellImHoveringOver.RowIndex).Cells(theCellImHoveringOver.ColumnIndex).Value.ToString() & _ "'", True) Catch ex As SqlException cellErrorText = "Can't look this cell up" cellErrorLocation = theCellImHoveringOver End Try End Sub Private Sub dataGridView1_CellErrorTextNeeded(ByVal sender As Object, _ ByVal e As DataGridViewCellErrorTextNeededEventArgs) _ Handles dataGridView1.CellErrorTextNeeded If (Not cellErrorLocation Is Nothing) Then If e.ColumnIndex = cellErrorLocation.ColumnIndex AndAlso _ e.RowIndex = cellErrorLocation.RowIndex Then e.ErrorText = cellErrorText End If End If End Sub Private Function Populate(ByVal query As String, ByVal resetUnsharedCounter As Boolean) As DataTable If resetUnsharedCounter Then ResetCounter() End If ' Alter the data source as necessary Dim adapter As New SqlDataAdapter(query, _ New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" & _ "Initial Catalog=Northwind;Data Source=localhost")) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture adapter.Fill(table) Return table End Function Private count As New Label() Private unsharedRowCounter As Integer Private Sub ResetCounter() unsharedRowCounter = 0 count.Text = unsharedRowCounter.ToString() End Sub
Available since 2.0
DataSource
VirtualMode
ShowCellErrors
DataGridViewCellErrorTextNeededEventHandler
DataGridViewCellErrorTextNeededEventArgs
DataGridViewCellErrorTextNeededEventArgs.ErrorText
DataGridViewCell.ErrorText
OnCellErrorTextNeeded
DataGridView Class
System.Windows.Forms Namespace
DataGridView Control (Windows Forms)
Best Practices for Scaling the Windows Forms DataGridView Control