For Textbox, you can use the keypress () event to Prevent Users From inputting invalid data.
The cell in the datagridview cannot be controlled through keypress (). Maybe I have not found a method.
The cellvalidating () method is used to determine when the user finishes editing. If the data is not properly restored.
Private Void Datagridview1_cellvalidating ( Object Sender, datagridviewcellvalidatingeventargs E)
{
// Editable column
If (E. columnindex! = 2 & E. columnindex! = 3 )
Return ;
Double Outdb = 0 ;
If ( Double . Tryparse (E. formattedvalue. tostring (), Out Outdb ))
{
E. Cancel = False ;
}
Else
{
E. Cancel = True ; // Restore if the data format is incorrect.
Datagridview1.canceledit ();
}
}
Keypress () event of textbox Private Void Txtk_keypress ( Object Sender, keypresseventargs E)
{
Double Outdb = 0 ;
If ( Double . Tryparse (txtk. Text + E. keychar. tostring (), Out Outdb ))
{
E. Handled = False ;
}
Else
{
E. Handled = True ;
}
}
I inherited textbox override keypress () and encapsulated a custom control.
URL: Success
Requirements:
Focus is not left after verification error.
Implementation:
You can use the dgv_details_cellvalidating event for cell verification.
When verification fails, call E. Cancel = true. When the event chain is terminated, the cell remains editable.
Call dgv_details.canceledit (); the cell content can be rolled to the value before modification.
Use System. Windows. Forms. sendkeys. Send ("^ A"); To select all cells.
Select and edit the cell status
Implementation:
// Obtain the focus of the datagridview.
Dgv_details.focus ();
// Specify the current cell in the datagridview.
Dgv_details.currentcell = dgv_details [0, 0];
// Start editing status
Dgv_details.beginedit (false );
Custom Auto generate bound Columns
Implementation:
Dgv_details.autogeneratecolumns = false;
Set the column background color
Implementation:
Color gridreadonlycolor = color. lightgoldenrodyellow;
Dgv_details.columns [1]. defaultcellstyle. backcolor =
Winkeys. gridreadonlycolor;
Design of cell verification for the datagridview
Question 1: Are you bound or not?
Binding advantages: relatively simple, Code Less.
Disadvantages of binding: Data in the datagridview is affected by the data source (primary key constraint and Value Type constraint ). When the input content is different, the dataerror event is triggered, and the input content cannot be saved to cells and data sources. For special verification (such as length and format), you still need to write code for implementation.
About adding rows. There is a problem with the Multi-primary key verification when a new row is added, and when the verification fails, all new rows will be deleted. There are many restrictions, which is inconvenient.
Advantage of non-binding: flexible verification and other processing. Data sources are not restricted.
Non-binding disadvantages: it requires a lot of code implementation to display and update data to the database, and the efficiency is relatively low.
In general, I prefer the non-binding mode when it is difficult to handle verification. If the data volume is large, it is better to use the binding mode when the verification is relatively simple.