<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6119672932545136692</id><updated>2012-02-16T10:59:26.903-08:00</updated><category term='C#'/><category term='Tips'/><title type='text'>Berry Software</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://berrysoft.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6119672932545136692/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://berrysoft.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Berry Software</name><uri>http://www.blogger.com/profile/09504878872717464724</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6119672932545136692.post-9159160680237038255</id><published>2010-12-08T17:06:00.000-08:00</published><updated>2010-12-08T17:25:48.173-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>DataGridView cell KeyPress event handler</title><content type='html'>&lt;span id="internal-source-marker_0.9143609151080234" style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;DataGridView doesn’t provide an event for cell key actions like  CellKeyPress, or CellKeyDown, etc for us to capture. The reason is that  there’re several kinds of cells including DataGridViewTextBoxCell,  DataGridViewComboBoxCell, DataGridViewCheckBoxCell and so on, and not  all of those cell types, however, has KeyPress event.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;However, there are 2 simple ways to get around this issue to catch cell KeyPress event.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;The two example below showing how to limit key pressed to digits&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;1. Catch EditingControlShowCating event of DataGridView&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0pt; margin-top: 0pt; text-indent: 36pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;private void gridData_EditingControlShowing(object sender, &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-indent: 36pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;DataGridViewEditingControlShowingEventArgs e)&lt;/span&gt;&lt;/div&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; txtBox = e.Control as TextBox;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (txtBox != null)&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; txtBox.KeyPress += txtBox_KeyPress;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;}&lt;/span&gt;&lt;/div&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private void txtBox_KeyPress(object sender, KeyPressEventArgs e)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!char.IsDigit(e.KeyChar) &amp;amp;&amp;amp; e.KeyChar != (char)Keys.Back)&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Handled = true;&lt;/span&gt;&lt;/div&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // detach the event from the current cell when it loses focus otherwise txtBox_KeyPress will launch mutiple times&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private void gridData_CellLeave(object sender, DataGridViewCellEventArgs e)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (txtBox != null)&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; txtBox.KeyPress -= txtBox_KeyPress;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: italic; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; }&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;2. Create a derived control from DataGridView and override ProcessDialogKey method:&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;Sample code:&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; public partial class MyGrid : DataGridView&lt;/span&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt; text-align: left; text-indent: 36pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt; text-align: left; text-indent: 36pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;private List&amp;lt;Keys&amp;gt; allowedKeys = new List&amp;lt;Keys&amp;gt; {Keys.D0, Keys.D1, Keys.D2, Keys.D3,&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt; text-align: left; text-indent: 36pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;Keys.D4,Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9};&lt;/span&gt;&lt;/div&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public MyGrid()&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : base()&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt; text-indent: 36pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; InitializeComponent();&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected override bool ProcessDialogKey(Keys keyData)&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool result = base.ProcessDialogKey(keyData);&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (allowedKeys.Contains(keyData) == false)&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = true;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 72pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return result;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;"&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;span style="background-color: transparent; color: black; font-family: Calibri; font-size: 10pt; font-style: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6119672932545136692-9159160680237038255?l=berrysoft.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://berrysoft.blogspot.com/feeds/9159160680237038255/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://berrysoft.blogspot.com/2010/12/datagridview-cell-keypress-event.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6119672932545136692/posts/default/9159160680237038255'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6119672932545136692/posts/default/9159160680237038255'/><link rel='alternate' type='text/html' href='http://berrysoft.blogspot.com/2010/12/datagridview-cell-keypress-event.html' title='DataGridView cell KeyPress event handler'/><author><name>Berry Software</name><uri>http://www.blogger.com/profile/09504878872717464724</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
