Usage of the carriage return key in the DataGridView

Source: Internet
Author: User
In NET, the DataGridView is a good control. It provides a custom table for inputting and displaying data. if you provide the DataGridView in your program as a way for users to input multi-row data, you may want to redefine the default operation of the enter key.
Assume that when you press the Enter key in the DataGridView, the cursor will move to the cell in which the same column is located (as shown by the Red Arrow), but when multiple rows of data are input, the better way to respond to the Enter key is to move to the first cell in the next row (Blue Arrow ).

To do this, you can use the class derived from the DataGridView: Public class Grid: DataGridView
{
Then override (override) The OnKeyUp protected method: Protected override void OnKeyUp (KeyEventArgs e)
{
If (e. KeyCode = Keys. Enter)
{
Int currentRow = this. CurrentRow. Index;
If (currentRow> = 0)
This. CurrentCell = this. Rows [currentRow]. Cells [0];
}
Base. OnKeyUp (e );
}

Of course, if you want to provide this capability in an existing DataGridView, you can simply sign the KeyUp event and execute the same code in the event handler ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.