keyevents in datagrid

Hallo,

I have a windows form with one treeview and two panels.In one panel have a datagrid. I am trying to capture the keyevents in the datagrid.When i use keydown eventhandler for the datagrid the keystrokes gets captured only if the rowheader or the pointer on left side of the grid.

I also tried ProcessCmdKey, but then the event gets captured anywhere in the form which i dont want to happen. Can anybody please help me

thanks n regards
swingme


Answer this question

keyevents in datagrid

  • Scott Lloyd

    Add a tablestyle to your datagrid.  Add a handler to the keypress events for the datagridtextboxcolumn's textbox to recieve the event.  Here is a vb.net sample for recieving the validate event

    http://www.windowsformsdatagridhelp.com/default.aspx ID=bece831d-6742-4364-bd0d-203ca99d2825



  • Rod B

    Here is some sample code



    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;

    namespace GridKeyPress
    {
     /// <summary>
     /// Summary description for Form1.
     /// </summary>
     public class Form1 : System.Windows.Forms.Form
     {
      private System.Windows.Forms.DataGrid dataGrid1;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      public Form1()
      {
       //
       // Required for Windows Form Designer support
       //
       InitializeComponent();

       //
       // TODO: Add any constructor code after InitializeComponent call
       //
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
       this.dataGrid1 = new System.Windows.Forms.DataGrid();
       ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
       this.SuspendLayout();
       //
       // dataGrid1
       //
       this.dataGrid1.DataMember = "";
       this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
       this.dataGrid1.Location = new System.Drawing.Point(14, 37);
       this.dataGrid1.Name = "dataGrid1";
       this.dataGrid1.Size = new System.Drawing.Size(418, 192);
       this.dataGrid1.TabIndex = 0;
       //
       // Form1
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(448, 266);
       this.Controls.Add(this.dataGrid1);
       this.Name = "Form1";
       this.Text = "Form1";
       this.Load += new System.EventHandler(this.Form1_Load);
       ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
       this.ResumeLayout(false);

      }
      #endregion

      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main()
      {
       Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e)
      {
       SqlConnection conn;
       SqlDataAdapter da;
       DataSet ds;
       conn = new SqlConnection("Server = KENSACER;Database = NorthWind; Integrated Security = SSPI;");
       da = new SqlDataAdapter("Select * from Categories", conn);
       ds = new DataSet();
       try
       {
        da.Fill(ds, "Categories");
        DataGridTableStyle ts;
        ts = new DataGridTableStyle();
        ts.MappingName = "Categories";
        
        DataGridTextBoxColumn colName;

        colName = new DataGridTextBoxColumn();
        colName.MappingName="CategoryName";
        colName.HeaderText = "Name";
        colName.Width = 80;
        colName.TextBox.KeyPress  += new KeyPressEventHandler(keypressed);
        DataGridTextBoxColumn colDescription;

        colDescription = new DataGridTextBoxColumn();
        colDescription.MappingName="Description";
        colDescription.HeaderText = "Description";
        colDescription.Width = 250;

        ts.GridColumnStyles.Add(colName);
        ts.GridColumnStyles.Add(colDescription);
        dataGrid1.TableStyles.Add(ts);
       }
       catch (Exception ex)
          {
           System.Diagnostics.Trace.WriteLine(ex.ToString());
          }

       dataGrid1.DataSource = ds.Tables["Categories"];
      }

      void keypressed(Object o, KeyPressEventArgs e)
      {
       System.Diagnostics.Trace.WriteLine(e.KeyChar);
      }

     }
    }


     



  • Jeremy Boschen

    hallo Ken,

    Its not working.Tongue Tied   I dont know what i do wrong. I tried with event handler for key down.But as i told only works if i first click on the table header or on the left rand.

    thanks n regards
    swingme

  • keyevents in datagrid