Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. DataGridView and Key events

DataGridView and Key events

Scheduled Pinned Locked Moved Windows Forms
csharp
12 Posts 3 Posters 68 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M michaelbarb

    None of the key events are working. I have tried several approached and none work. I m using Windows 10, .NET 4.8.

    So many years of programming I have forgotten more languages than I know.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    You need to explain what not working" actually means. As it stands it is anyone's guess what you are doing wrong.

    M 1 Reply Last reply
    0
    • L Lost User

      Before you complain select is broken, do include a sample. Otherwise, not looking at it :thumbsup:

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      M Offline
      M Offline
      michaelbarb
      wrote on last edited by
      #4

      There is not much more to say. Create a new windows form project. Put a DataGridView in the form. Go to the events for the DataGridView. Double click any or all of the key events. Go to the code behind. Put trivial line in each event (int i = 0; i = I+1;) and set a break point. You will never trip the key events. I tried each event without the others present. As a last test I added event "CellValueChanged" to see if I was getting some event and it worked.

      namespace DataGridView_Events
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      InitializeTable();
      }

          private void InitializeTable()
          {
              dataGridView1.ColumnCount = 3;
              dataGridView1.Columns\[0\].Name = "Column0";
              dataGridView1.Columns\[1\].Name = "Column1";
              dataGridView1.Columns\[2\].Name = "Column2";
      
              dataGridView1.Rows.Add(5);
              dataGridView1.Rows\[0\].HeaderCell.Value = "Row1";
              dataGridView1.Rows\[1\].HeaderCell.Value = "Row2";
              dataGridView1.Rows\[2\].HeaderCell.Value = "Row3";
      
              for (int i = 0; i < 3; i++)
              {
                  dataGridView1.Rows\[i\].Cells\[0\].Value = 0 + i;// TheTrayHandler.Axis\[i\].MaxSpeed;
                  dataGridView1.Rows\[i\].Cells\[1\].Value = 10 + i;// TheTrayHandler.Axis\[i\].SpeedPercent;
                  dataGridView1.Rows\[i\].Cells\[2\].Value = 20 + i;// TheTrayHandler.Axis\[i\].HighAccel;
              }
              dataGridView1.Refresh();
          }
      
          private void dataGridView1\_KeyPress(object sender, KeyPressEventArgs e)
          {
              int i = 0; i = i + 1;
          }
      
          private void dataGridView1\_KeyDown(object sender, KeyEventArgs e)
          {
              int i = 0; i = i + 1;
          }
      
          private void dataGridView1\_KeyUp(object sender, KeyEventArgs e)
          {
              int i = 0; i = i + 1;
          }
      
          private void dataGridView1\_CellValueChanged(object sender, DataGridViewCellEventArgs e)
          {
              int i = 0; i = i + 1;
          }
      }
      

      }

      So many years of programming I have forgotten more languages than I know.

      L 1 Reply Last reply
      0
      • M michaelbarb

        There is not much more to say. Create a new windows form project. Put a DataGridView in the form. Go to the events for the DataGridView. Double click any or all of the key events. Go to the code behind. Put trivial line in each event (int i = 0; i = I+1;) and set a break point. You will never trip the key events. I tried each event without the others present. As a last test I added event "CellValueChanged" to see if I was getting some event and it worked.

        namespace DataGridView_Events
        {
        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();
        InitializeTable();
        }

            private void InitializeTable()
            {
                dataGridView1.ColumnCount = 3;
                dataGridView1.Columns\[0\].Name = "Column0";
                dataGridView1.Columns\[1\].Name = "Column1";
                dataGridView1.Columns\[2\].Name = "Column2";
        
                dataGridView1.Rows.Add(5);
                dataGridView1.Rows\[0\].HeaderCell.Value = "Row1";
                dataGridView1.Rows\[1\].HeaderCell.Value = "Row2";
                dataGridView1.Rows\[2\].HeaderCell.Value = "Row3";
        
                for (int i = 0; i < 3; i++)
                {
                    dataGridView1.Rows\[i\].Cells\[0\].Value = 0 + i;// TheTrayHandler.Axis\[i\].MaxSpeed;
                    dataGridView1.Rows\[i\].Cells\[1\].Value = 10 + i;// TheTrayHandler.Axis\[i\].SpeedPercent;
                    dataGridView1.Rows\[i\].Cells\[2\].Value = 20 + i;// TheTrayHandler.Axis\[i\].HighAccel;
                }
                dataGridView1.Refresh();
            }
        
            private void dataGridView1\_KeyPress(object sender, KeyPressEventArgs e)
            {
                int i = 0; i = i + 1;
            }
        
            private void dataGridView1\_KeyDown(object sender, KeyEventArgs e)
            {
                int i = 0; i = i + 1;
            }
        
            private void dataGridView1\_KeyUp(object sender, KeyEventArgs e)
            {
                int i = 0; i = i + 1;
            }
        
            private void dataGridView1\_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                int i = 0; i = i + 1;
            }
        }
        

        }

        So many years of programming I have forgotten more languages than I know.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        keypress event in Datagridview[^]

        M 1 Reply Last reply
        0
        • L Lost User

          keypress event in Datagridview[^]

          M Offline
          M Offline
          michaelbarb
          wrote on last edited by
          #6

          Thanks. I needed to add the following to pass the event up:

              private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
              {
                  e.Control.KeyPress += new KeyPressEventHandler(dataGridView1\_KeyPress);
              }
          

          So many years of programming I have forgotten more languages than I know.

          1 Reply Last reply
          0
          • L Lost User

            You need to explain what not working" actually means. As it stands it is anyone's guess what you are doing wrong.

            M Offline
            M Offline
            michaelbarb
            wrote on last edited by
            #7

            Now how do I do it when I put it in a custom control.

               protected override void OnKeyPress(KeyPressEventArgs e)
                {
                    // Key checking
                    base.OnKeyPress(e);
                }
            
                protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
                {
                    e.Control.KeyPress += new KeyPressEventHandler(OnKeyPress);
                    base.OnEditingControlShowing(e);
                }
            

            I get an error on the OnKeyPress in setting the event handler: No overload for 'OnKeyPress' matches delegate in 'KeyPressEventHandler'

            So many years of programming I have forgotten more languages than I know.

            Richard DeemingR L 2 Replies Last reply
            0
            • M michaelbarb

              Now how do I do it when I put it in a custom control.

                 protected override void OnKeyPress(KeyPressEventArgs e)
                  {
                      // Key checking
                      base.OnKeyPress(e);
                  }
              
                  protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
                  {
                      e.Control.KeyPress += new KeyPressEventHandler(OnKeyPress);
                      base.OnEditingControlShowing(e);
                  }
              

              I get an error on the OnKeyPress in setting the event handler: No overload for 'OnKeyPress' matches delegate in 'KeyPressEventHandler'

              So many years of programming I have forgotten more languages than I know.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #8

              Simple:

              protected override void OnKeyPress(KeyPressEventArgs e)
              {
              base.OnKeyPress(e);
              HandleKeyPress(this, e);
              }

              protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
              {
              base.OnEditingControlShowing(e);
              e.Control.KeyPress += HandleKeyPress;
              }

              protected virtual void HandleKeyPress(object sender, KeyPressEventArgs e)
              {
              // Key checking...
              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              M 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Simple:

                protected override void OnKeyPress(KeyPressEventArgs e)
                {
                base.OnKeyPress(e);
                HandleKeyPress(this, e);
                }

                protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
                {
                base.OnEditingControlShowing(e);
                e.Control.KeyPress += HandleKeyPress;
                }

                protected virtual void HandleKeyPress(object sender, KeyPressEventArgs e)
                {
                // Key checking...
                }


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                M Offline
                M Offline
                michaelbarb
                wrote on last edited by
                #9

                Beautiful, Thank you. The event OnKeyPress never gets used.

                So many years of programming I have forgotten more languages than I know.

                1 Reply Last reply
                0
                • M michaelbarb

                  Now how do I do it when I put it in a custom control.

                     protected override void OnKeyPress(KeyPressEventArgs e)
                      {
                          // Key checking
                          base.OnKeyPress(e);
                      }
                  
                      protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
                      {
                          e.Control.KeyPress += new KeyPressEventHandler(OnKeyPress);
                          base.OnEditingControlShowing(e);
                      }
                  

                  I get an error on the OnKeyPress in setting the event handler: No overload for 'OnKeyPress' matches delegate in 'KeyPressEventHandler'

                  So many years of programming I have forgotten more languages than I know.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  The documentation is quite clear: KeyPressEventHandler Delegate (System.Windows.Forms) | Microsoft Docs[^]

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    The documentation is quite clear: KeyPressEventHandler Delegate (System.Windows.Forms) | Microsoft Docs[^]

                    M Offline
                    M Offline
                    michaelbarb
                    wrote on last edited by
                    #11

                    It is clear if you can find it by guessing the right search que. Thanks again.

                    So many years of programming I have forgotten more languages than I know.

                    L 1 Reply Last reply
                    0
                    • M michaelbarb

                      It is clear if you can find it by guessing the right search que. Thanks again.

                      So many years of programming I have forgotten more languages than I know.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      You can see the keyword KeyPressEventArgs in your code, so that should be a starting point.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups