. NET 2.0-winform control-datagridview programming 36 (2)
Directory:
- ① Setting indicated by the error icon
- ② Cell input force is worth verifying
- ③ Capture when the user's input value is incorrect
① Datagridview error icon:
Go to top
To remind users, the error icon can be used to highlight the datagridview. For example:
The error icon can be displayed in cells and row headers, but cannot be displayed on column heads.
1) errortext attributes
When the errortext attribute of a cell/row is set, the error icon of the cell/row is displayed. In addition, the error icon is displayed only when datagridview. showcellerrors = true. (True by default)
[VB. NET]
'Set the cell (0, 0) to indicate the error icon.
Datagridview1 (0, 0). errortext = "confirm the cell value. "
'Set the row header of row 4th (Index = 3) to display the error icon.
Datagridview1.rows (3). errortext = "you cannot enter a negative value. "
2) cellerrortextneeded and rowerrortextneeded events
Indicates the error icon during instant input. You can use the cellerrortextneeded event.
At the same time, when processing a large amount of data, you need to perform multiple Content checks and display the error icon in the application. Setting errortext by traversing cells is inefficient. The cellerrortextneeded event should be used. The rowerrortextneeded event should be used to set the row error icon.
However, when the datasource property is set to virtualmode = true, the above event will not be thrown.
[VB. NET]
'Cellerrortextneeded event handling method
Private sub maid (byval sender as object ,_
Byval e as maid )_
Handles datagridview1.cellerrortextneeded
Dim dgv as datagridview = ctype (sender, datagridview)
'When the cell value is a negative integer, the error icon is displayed.
Dim cellval as object = dgv (E. columnindex, E. rowindex). Value
If typeof cellval is Integer andalso CINT (cellval) <0 then
E. errortext = "you cannot enter a negative integer. "
End if
End sub
'Rowerrortextneeded event handling method
Private sub maid (byval sender as object ,_
Byval e as datagridviewrowerrortextneededeventargs )_
Handles datagridview1.rowerrortextneeded
Dim dgv as datagridview = ctype (sender, datagridview)
If dgv ("column1", E. rowindex). value is dbnull. Value andalso _
Dgv ("column2", E. rowindex). value is dbnull. value then
E. errortext = _
"Column1 and column2 must enter a value. "
End if
End sub
[C #]
// Cellerrortextneeded event handling method
Private void maid (Object sender,
Datagridviewcellerrortextneededeventargs E)
{
Datagridview dgv = (datagridview) sender;
// When the cell value is a negative integer, the error icon is displayed.
Object cellval = dgv [E. columnindex, E. rowindex]. value;
If (cellval is Int & (INT) cellval) <0)
{
E. errortext = "you cannot enter a negative integer. ";
}
}
// Rowerrortextneeded event handling method
Private void maid (Object sender,
Datagridviewrowerrortextneededeventargs E)
{
Datagridview dgv = (datagridview) sender;
If (dgv ["column1", E. rowindex]. value = dbnull. Value &&
Dgv ["column2", E. rowindex]. value = dbnull. value)
{
E. errortext =
"Column1 and column2 must enter a value. ";
}
}
②Verify the force value of the datagridview cell:
If you want to verify the force after a cell is loaded and cancel the action after it is canceled in an incorrect case, use the event cellvalidating. The following code shows that when "column1" is empty, it is set as an error icon in this row and the action is canceled. (The focus cannot leave the cell)
[VB. NET]
'Cellvalidating event handling method
Private sub maid (byval sender as object ,_
Byval e as datagridviewcellvalidatingeventargs )_
Handles datagridview1.cellvalidating
Dim dgv as datagridview = ctype (sender, datagridview)
If dgv. Columns (E. columnindex). Name = "column1" andalso _
E. formattedvalue. tostring () = "" then
'Row error prompt settings
The dgv. Rows (E. rowindex). errortext = "value is not input. "
'Cancel the input content and restore it to the previous input content.
'Dgv. canceledit ()
'Action after cancellation
E. Cancel = true
End if
End sub
'Cellvalidated event handling method
Private sub maid (byval sender as object ,_
Byval e as datagridviewcelleventargs )_
Handles datagridview1.cellvalidated
Dim dgv as datagridview = ctype (sender, datagridview)
'If the verification is passed, the error message indicating clearing the row will be displayed.
Dgv. Rows (E. rowindex). errortext = nothing
End sub
③Error capture processing when the user input in the datagridview is incorrect
For example, if an incorrect value is entered in a numeric column, an icon is displayed. However, the icon indicates that the user will not be notified immediately.
Another processing method is introduced here: bind a dataerror event for processing.