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

asked on

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
Avatar of rwheeler23
rwheeler23
Flag of United States of America image

ASKER

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

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

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

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

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

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
I did correct the if statement to read as you suggested:

 if (row.Cells[0].Value.ToString().StartsWith(PaBillingEntryWindow.CustomerNumber.Value))
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of ankitkumar29
ankitkumar29
Flag of India 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
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?
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
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.
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;
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