How to fire keypress VS 2008 C#

Posted on 2010-03-27
Medium Priority
Last Modified: 2012-05-09
I have added a keypress event on my datagridview in an attmept to enable a progressive list search. It is down near the bottom of this code:

 private void dgvAgenciesByJobs_Keypress(object sender, System.Windows.Forms.KeyEventArgs e)

I tried putting a breakpoint at the beginning but if never appears to fire. Is there something I have to do besides just typing it in to get it to be recognized? What are all the steps necessary to add a keypress event in C# VS 2008?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using Microsoft.Dexterity.Applications.DynamicsDictionary;
using Microsoft.Dexterity.Applications.ProjectAccountingDictionary;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Dexterity.Bridge;
using Microsoft.Dexterity.Applications;
using Microsoft.Dexterity.Shell;

namespace ViewAgenciesByJob
    public partial class ViewAgenciesByJobs : DexUIForm
        // Create a reference to the Project Billing Entry Window
        static PaBillingEntryForm PaBillingEntryForm = ProjectAccounting.Forms.PaBillingEntry;
        static PaBillingEntryForm.PaBillingEntryWindow PaBillingEntryWindow = PaBillingEntryForm.PaBillingEntry;

        /* Setup PA Billing Entry form so you send back value for Customer ID */
        private System.Data.SqlClient.SqlConnection jobconnection;
        private System.Data.DataSet AgenciesByJobsDataSet;
        private System.Data.SqlClient.SqlCommand AgenciesByJobsCommand;
        private System.Data.SqlClient.SqlDataAdapter DataAdapter;

        /* Define connection string */
        string connectionString = "";
        string strSearch = "";
        int i;
        public ViewAgenciesByJobs(string cs)
            connectionString = cs;

                /* Define the data set for Agencies by Jobs */
                jobconnection = new System.Data.SqlClient.SqlConnection(connectionString);

                /* Open the connection */

                AgenciesByJobsDataSet = new System.Data.DataSet();
                AgenciesByJobsDataSet.CaseSensitive = false;

                AgenciesByJobsCommand = new System.Data.SqlClient.SqlCommand();
                AgenciesByJobsCommand.Connection = jobconnection;

                AgenciesByJobsCommand.CommandText = "SELECT JOBNUMBER,AGENCY FROM JOBS ORDER BY JOBNUMBER";

                DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                DataAdapter.SelectCommand = AgenciesByJobsCommand;
                DataAdapter.TableMappings.Add("Table", "AgenciesByJobs");

                dgvAgenciesByJobs.ReadOnly = false;
                dgvAgenciesByJobs.RowHeadersVisible = false;
                dgvAgenciesByJobs.AllowUserToResizeColumns = false;
                dgvAgenciesByJobs.AllowUserToResizeRows = false;
                dgvAgenciesByJobs.DataSource = AgenciesByJobsDataSet.Tables["AgenciesByJobs"].DefaultView;
                dgvAgenciesByJobs.Columns[0].Width = 85;
                dgvAgenciesByJobs.Columns[0].HeaderText = "Job Number";
                dgvAgenciesByJobs.Columns[0].ReadOnly = true;
                dgvAgenciesByJobs.Columns[1].Width = 95;
                dgvAgenciesByJobs.Columns[1].HeaderText = "Agency Code";
                dgvAgenciesByJobs.Columns[1].ReadOnly = true;
            catch (Exception ex)

            /* Close the job connection */
            catch(Exception e)

        private void btnExit_Click(object sender, EventArgs e)

        private void dgvAgenciesByJobs_CellContentClick(object sender, DataGridViewCellEventArgs e)
            int CurrentRow;

            CurrentRow = dgvAgenciesByJobs.CurrentCell.RowIndex;
            dgvAgenciesByJobs.CurrentCell = dgvAgenciesByJobs[1, CurrentRow];
            PaBillingEntryWindow.CustomerNumber.Value = dgvAgenciesByJobs.CurrentCell.Value.ToString();

        private void dgvAgenciesByJobs_Keypress(object sender, System.Windows.Forms.KeyEventArgs e)
            strSearch += e.KeyCode;
            for (i = 0; i <= dgvAgenciesByJobs.RowCount - 1; i++)
                if (dgvAgenciesByJobs[0, i].Value != null && dgvAgenciesByJobs[0, i].Value.ToString().StartsWith(strSearch))
                    dgvAgenciesByJobs.Rows[i].Selected = true;
                    dgvAgenciesByJobs.CurrentCell = dgvAgenciesByJobs[0, i];
                    if (dgvAgenciesByJobs.Rows[i].Displayed == false)
                        dgvAgenciesByJobs.FirstDisplayedScrollingRowIndex = i;

        private void ViewAgenciesByJobs_Load(object sender, EventArgs e)


Open in new window

Question by:rwheeler23
  • 2
  • 2
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 28800626
Did you actually tie the event handler function to the the event? By this I mean, you have to select your DataGridView in the designer, click on the lightning bolt in the Properties window to bring up the list of events, scroll down to the KeyPress event, and click the drop-down to select the method you created in your post.

Author Comment

ID: 28801987
That was it. I am new to VS and I knew there had to be some simple trick to that. Thank you.

After I made this change, I noticed the same issue I had with my CellContentClick event. I can click on a row 9 times and it does exactly what it needs to do, which is to return the Agency code associated with that Job and then I click the 10th time and it does nothing. I then move the scroll bar and click on a row and it works again. The rows are actually highlighted so it appears the focus is changing. How can I troubleshoot this behavior?
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 29005819
Do you have some logic that is checking the number of clicks (e.g. on 10th click, do something else)?

Author Comment

ID: 29016796
No, it will work for 20 clicks and then stops, then it works for 2 clicks and then stops, it is completely random. I think what I will do is add a Select that exits and returns whever the user is currently sitting on. Perhaps I can set up a double click event as well.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question