Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwheeler23

ASKER

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?
Do you have some logic that is checking the number of clicks (e.g. on 10th click, do something else)?
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.