473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

WinForm - DataGrid - Enter Key

For a Data Grid in a Win Form, when the user selects a row and then presses
the Enter key, how do I capture that the user pressed the enter key?
Nov 17 '05 #1
3 7256
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to handle the key
press event in a DataGrid. If there is any misunderstanding, please feel
free to let me know.

This depends on how your datagrid looks like. If the selection is to the
whole row (The whole row is highlighted), you can handle the
DataGrid.KeyDown event to achieve this.

private void dataGrid1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
}

When the focus is in a datagrid cell, the above keydown event will not get
fired. We need to do the following:

Add this line in InitializeComponent(),

this.dataGrid1.ControlAdded +=new
ControlEventHandler(dataGrid1_ControlAdded);

Then add this:

private void dataGrid1_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.KeyDown +=new KeyEventHandler(Control_KeyDown);
}

private void Control_KeyDown(object sender, KeyEventArgs e)
{

}

However, in this case, we can use the above method to handle all the key
event except the Enter key and some other control keys. Because the event
is NOT fired when Enter key is pressed. This is by design. So I suggest you
stick to the first method to handle DataGrid.KeyDown event.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #2
How do I force the Data Grid to only select rows and not select cells? If
the user can only select rows this will work for me because I'm using the
Data Grid to show available License Dealers to select from.

Thanks for your last response, it was easy to understand and worked. If
only I can set the Data Grid to select rows only then it will be perfect.
"Kevin Yu [MSFT]" wrote:
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to handle the key
press event in a DataGrid. If there is any misunderstanding, please feel
free to let me know.

This depends on how your datagrid looks like. If the selection is to the
whole row (The whole row is highlighted), you can handle the
DataGrid.KeyDown event to achieve this.

private void dataGrid1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
}

When the focus is in a datagrid cell, the above keydown event will not get
fired. We need to do the following:

Add this line in InitializeComponent(),

this.dataGrid1.ControlAdded +=new
ControlEventHandler(dataGrid1_ControlAdded);

Then add this:

private void dataGrid1_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.KeyDown +=new KeyEventHandler(Control_KeyDown);
}

private void Control_KeyDown(object sender, KeyEventArgs e)
{

}

However, in this case, we can use the above method to handle all the key
event except the Enter key and some other control keys. Because the event
is NOT fired when Enter key is pressed. This is by design. So I suggest you
stick to the first method to handle DataGrid.KeyDown event.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #3
Hi,

You can check the following article for how to select the whole row when
click on a WinForm DataGrid.

http://www.syncfusion.com/FAQ/Window...44c.aspx#q689q

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: zbcong | last post by:
hello in my winform,there is a datagrid,i want to fire some actions when the selected index is changed,but i fail to find the SelectedIndexChanged event to the winform datagrid,why? how can i...
3
by: Dav | last post by:
I have a Winform with a derived datagrid and a button that adds a row to the datagrid in its Click event. The button has a shortcut text "&New Row". When I enter a value in a datagrid cell and...
1
by: Udi | last post by:
Hi everyone! I have a major problem with the DataGrid control for the Windows Form! I am using the DataAdapter to get data from Sql Server 2000 DataBase and populating a DataGrid with the Table i...
1
by: zbcong | last post by:
hello in my winform,there is a datagrid,i want to fire some actions when the selected index is changed,but i fail to find the SelectedIndexChanged event to the winform datagrid,why? how can i...
1
by: Vinicius | last post by:
I am building a form with a datagrid that should only show vertical grid lines. The problem is that I couldn't find a way to do this. Does anybody knows how it could be done (i am using a winform...
3
by: adh | last post by:
Please refer me to a well developed WinForm Hierarchical DataGrid sample code. I would like to save the cost and troubles of an 3'rd party winforms grid. Thanks, adh
0
by: Serdge Kooleman | last post by:
Winform (DataGrid) when i'm editing the text in the curent cell, i need to change color/font/background in it. thank you
1
by: Jon B | last post by:
Hi All! I would like to make my WinForm DataGrid control so that user could easily edit the items in the **related table**. For instance, I have customers table and it has one-to-many...
1
by: siqram | last post by:
I am trying to find out the actual column width that is available in a winform datagrid in .NET framework 1.1 Width property gives the width of the datagrid. but i want to autoresize my clumns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.