Today we're going to do a dev gridcongtrol detection of the validity of the input cell content, where null is not allowed, and the value of the column is not allowed to repeat as an example. (It can also be used in other ways, but not in other ways: when the cell value does not meet the criteria, the entire GridView is locked, except for the cell, where the input is not allowed, and the focus is locked in this cell).
1. The column to be validated first is set to the Repositoryitemcheckedit type, or another control type (the remaining control types are not tested), the following is an example of the Repositoryitemcheckedit type
2, find Repositoryitemcheckedit validating event, the specific code is as follows:
<span style= "FONT-SIZE:14PX;" > private void Textedit1_validating (object sender, System.ComponentModel.CancelEventArgs e) {Baseedi T textEdit = sender as Baseedit; if (textEdit.Text.ToString (). Trim (). Length = = 0) {E.cancel = true; Identification Error hint Errorreason = 0; Return } else {//Get all selected line numbers in the GridView///No multiple selections are allowed here, so there is only one row int[] IR Owid = This.gViewActList.GetSelectedRows (); for (int i = 0; i < Gviewactlist.datarowcount; i++) {//Repeat test, no validation when forward if (i! = Irowid[0]) {//textedit.editvalue currently edited value//required Note that the value of the current cell is taken: Textedit.editvalue//gridview did not update the value you entered when the textedit1_validating was executed, but after the method is executed, even if the condition is not met, The value you enter will also be updated to the GridView//and also indicate that in the validating functionWhen you exclude duplicate values, you get the value of the current row through the GridView, not the latest, that is, the IF (textEdit.EditValue.ToString () that you entered last time. Trim () = = Gviewactlist.getdatarow (i) ["Column name bound by GridView]". ToString (). Trim ()) {E.cancel = true; Identification Error hint Errorreason = 1; Return }}}}</span>
3, according to the identification in the validating event, error message prompt:
<span style= "FONT-SIZE:14PX;" >private void Gviewactlist_invalidvalueexception (object sender, Invalidvalueexceptioneventargs e) { if ( Errorreason = = 0) { e.errortext = "Action name is not allowed to be empty! "; } else if (Errorreason = = 1) { e.errortext = "Action name is not allowed to repeat! "; } else { E.errortext = "Value is invalid! "; } } </span>
The specific results are as follows:
Small bet
1, using the Repositoryitem.validating event, the "sender" of the event must be converted to the Baseedit type, using EditValue to obtain the current input value and verify, if the checksum does not pass, put E. Cancel sets true. This method is typically used for data validation of the cells of the built-in controls.
2. Using the Gridview.validatingeditor Event
The "sender" of the event must be converted to the GridView type, the current column can be obtained from the Gridview.focusedcolumn property, the value can be obtained from E.value, if the checksum does not pass, you need to set the E.valid to false: This method is typically used for data validation of text boxes within the entire grid.
Specific error messages can be handled in the Gridview.invalidvalueexception event
Dev Gridcontrol validation method for cell input data