How to fire keypress VS 2008 C#

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;
            
            InitializeComponent();

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

                /* Open the connection */
                jobconnection.Open();

                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");
                DataAdapter.Fill(AgenciesByJobsDataSet);

                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)
            {
                MessageBox.Show(ex.Message);
            }

            /* Close the job connection */
            try
            {
                jobconnection.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
              
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }

        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;
                    }
                    return;
                }
            }
        }

        private void ViewAgenciesByJobs_Load(object sender, EventArgs e)
        {

        }
    }
}

Open in new window

LVL 1
rwheeler23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwheeler23Author Commented:
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?
0
käµfm³d 👽Commented:
Do you have some logic that is checking the number of clicks (e.g. on 10th click, do something else)?
0
rwheeler23Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.