Visual Studio Tools 2008 Sort data grid view

I have created this form with a datagridview. The one last piece I need is to allow the user to begin entering a value for a job number(first column) and then hae it move to the first matching record. Can this be done within my existing code or should have used a difeerent control?
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 = "";
        
        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 = true;
                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[1].Width = 95;
                dgvAgenciesByJobs.Columns[1].HeaderText = "Agency Code";

                /* Extract customer number value from current row to pass back to Billing Entry screen */
                /* CustomerNumber = AgenciesByJobsDataSet.Tables["AgenciesByJobs"].Rows[0]["AGENCY"].ToString(); */
                /* CustomerNumber = dgvAgenciesByJobs.CurrentCell.Value.ToString(); */
                /* PaBillingEntryWindow.CustomerNumber.Value = dgvAgenciesByJobs.CurrentCell.Value.ToString(); */
                
            }
            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();
        }


    }
}

Open in new window

AgenciesByJobs.jpg
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.

rwheeler23Author Commented:
I added this snippet of build and my program builds but it always lands on the first record of the datagridview. What am I missing to get this to land on the record containing value sent down to it?
strSearch = PaBillingEntryWindow.CustomerNumber.Value;
                for (i = 0; i <= dgvAgenciesByJobs.RowCount - 1; i++)
                {
                    dgvAgenciesByJobs.CurrentCell = dgvAgenciesByJobs[0, i]; 
                    if (dgvAgenciesByJobs.CurrentCell.Value.ToString() != null && dgvAgenciesByJobs.CurrentCell.Value.ToString().StartsWith(strSearch))
                    {
                        dgvAgenciesByJobs.Rows[i].Selected = true;
                        dgvAgenciesByJobs.CurrentCell = dgvAgenciesByJobs[0, i];
                        if (dgvAgenciesByJobs.Rows[i].Displayed == false)
                        {
                            dgvAgenciesByJobs.FirstDisplayedScrollingRowIndex = i;
                        }
                        break;
                    }

                }

Open in new window

0
ankitkumar29Commented:
This should work for you.
foreach (DataGridViewRow row in dgvAgenciesByJobs.Rows)
            {
                if (row.Cells[0].Value.ToString() == PaBillingEntryWindow.CustomerNumber.Value)
                {   
                    row.Selected = true;
                    dgvAgenciesByJobs.CurrentCell = row.Cells[0];
                    break;
                }

            }

Open in new window

0
rwheeler23Author Commented:
I have tried placing this code in several different places but it always goes to the first record. Can you please postion the code in the proper location?
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);

                foreach (DataGridViewRow row in dgvAgenciesByJobs.Rows)
                {
                    if (row.Cells[0].Value.ToString() == PaBillingEntryWindow.CustomerNumber.Value)
                    {
                        row.Selected = true;
                        dgvAgenciesByJobs.CurrentCell = row.Cells[0];
                        break;
                    }
                }

                dgvAgenciesByJobs.ReadOnly = true;
                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[1].Width = 95;
                dgvAgenciesByJobs.Columns[1].HeaderText = "Agency Code";
                                                        
            }
            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();
        }


    }
}

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

ankitkumar29Commented:
Put the code from line 81 onwards of the above code. And the if should be as below if you want close match besides exact match. Also put a breakpoint within this if, I want to see if it is actually getting validated or not.
if (row.Cells[0].Value.ToString().StartsWith(PaBillingEntryWindow.CustomerNumber.Value))

Open in new window

0
rwheeler23Author Commented:
If I do that, when I run it I get "Object reference not set to an instance of an object"
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 = true;
                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[1].Width = 95;
                dgvAgenciesByJobs.Columns[1].HeaderText = "Agency Code";

                foreach (DataGridViewRow row in dgvAgenciesByJobs.Rows)
                {
                    if (row.Cells[0].Value.ToString() == PaBillingEntryWindow.CustomerNumber.Value)
                    {
                        row.Selected = true;
                        dgvAgenciesByJobs.CurrentCell = row.Cells[0];
                        break;
                    }
                }
                                                        
            }
            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();
        }


    }
}

Open in new window

0
ankitkumar29Commented:
try putting a breakpoint within the if to see if it is going there. my feeling is, for some reason the if isnt getting validated to true, which is causing the exception as well as not setting the cell you want as current.
You need to check the actual value and lengths the strings of PaBillingEntryWindow.CustomerNumber.Value  and row.Cells[0].Value.ToString() because my feeling is this is the reason your code isnt working as you'd like
0
rwheeler23Author Commented:
I did correct the if statement to read as you suggested:

 if (row.Cells[0].Value.ToString().StartsWith(PaBillingEntryWindow.CustomerNumber.Value))
0
ankitkumar29Commented:
It doesnt matter much if it starts with or is equal to. I would suggest you put a messagebox displaying the strings PaBillingEntryWindow.CustomerNumber.Value and row.Cells[0].Value.ToString() and their lengths just before the if.
0
rwheeler23Author Commented:
It it is telling me the value of PaBillingEntryWindow.CustomerNumber.Value is "" even though I have typed a value in the field on the screen.
0
ankitkumar29Commented:
And therein lies the problem. What kind of field are you using to display PaBillingEntryWindow.CustomerNumber?
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:
I am adding this lookup to an external program of which I have no control except to add VS snippets to it. I think what is happening is that even if I type a value into the field, unless I move beyond that field, the value is not committed so in my code it is blank.

I am thinking perhaps what is better here is to allow the user to start typing in the first column and then find the first match. So if they type 135 then we go to the first job that begins with 135. I assume I need to set my datagridview to not be read only, but I do not want either column to be editable. Given that you have all of my code what adjustments would you make to enable it to work this way?
0
ankitkumar29Commented:
What you are suggesting seems unlikely. Are you sure the property for accessing PaBillingEntryWindow.CustomerNumber 's text is .value and not .text? Controls have event handlers, textbox and label both have the TextChanged event so you can use the equivalent for PaBillingEntryWindow.CustomerNumber and once that event is called, it means the Customer Number is loaded and then you can load the dgvAgenciesByJobs gridview
0
rwheeler23Author Commented:
This is only about the 4th one I have written for this external program but it is the first one where I am trying to call my lookup window before moving past the data field on the screen. I tried inserting

MessageBox.Show(PaBillingEntryWindow.CustomerNumber.Value) or
MessageBox.Show(PaBillingEntryWindow.CustomerNumber.Value.ToString())

The value appears only after I move beyond the field.
As I type PaBillingEntryWindow.CustomerNumber. I do not get Text as an option

I will keep trying.
0
rwheeler23Author Commented:
This is how the screen gets referenced.

// Create a reference to the Project Billing Entry Window
static PaBillingEntryForm PaBillingEntryForm = ProjectAccounting.Forms.PaBillingEntry;
static PaBillingEntryForm.PaBillingEntryWindow PaBillingEntryWindow = PaBillingEntryForm.PaBillingEntry;
0
rwheeler23Author Commented:
I typed in this function in an attempt to get this to work. I just set a breakpoint on this function and it never fires. Is there something I need to do in the project to get it to recognize this routine? All I did was type it in, I did not do anything in the project to start typing in this routine.
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;
                }
            }
        }

Open in new window

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
.NET Programming

From novice to tech pro — start learning today.