Blog Home  Home Feed your aggregator (RSS 2.0)  
.Net Jonesie - Winforms DataGridView and Controlling keyboard entry
A simple programmers blog
 
# Tuesday, August 02, 2005

I have a DataGridView where I allow users to type in cell values, but I only want upper case.  To do this, you need to capture the control as it's being created and assign a KeyPress handler.  E.g:

    private void MyGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
      if (e.Control is DataGridViewTextBoxEditingControl)
      {
        e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);
      }
    }


    void Control_KeyPress(object sender, KeyPressEventArgs e)
    {
      e.KeyChar = e.KeyChar.ToString().ToUpper()[0];
    }

Of course, you can extend this further to do all sort of things, but if your requirements are complex then it's probably advisable to create your own custom column.

Tuesday, August 02, 2005 10:09:30 AM (New Zealand Standard Time, UTC+12:00)  #    Comments [2]   Visual Studio  | 
Wednesday, March 22, 2006 11:17:10 PM (New Zealand Standard Time, UTC+12:00)
Hello
I'm looking for a way to replace a "." for "," in a datagridview with keypress handler. Do you know how to do that? I'm using Visual Basic 2005. Hope you can help

thanks !
Bert
Bert Verburg
Wednesday, May 03, 2006 7:47:01 PM (New Zealand Standard Time, UTC+12:00)
Hi Bert,

I hope this code may be will help you to do that :

Private Sub mydatagridView_EditingControlShowing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
Handles mydatagridView.EditingControlShowing
If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
AddHandler e.Control.KeyPress, _
New KeyPressEventHandler(AddressOf Control_KeyPress)
End If
End Sub

Private Sub Control_KeyPress(ByVal sender As Object, _
ByVal e As KeyPressEventArgs)
If e.KeyChar = "," Then e.KeyChar = "."
End Sub


Regards,

Budi Sentosa
Indonesian VB 2005 Programmer.
Comments are closed.
Copyright © 2012 Peter G Jones. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: