Avatar of rwheeler23
rwheeler23
Flag for United States of America asked on

Using a datagridview as a lookup window

I have a WIndows form in a C# application with a datagrid view called dgvCustomers.  I now need to turn this into a lookup window so it will return the Customer number. There are only two fields on this form, CUSTNMBR and CUSTNAME. I am thinking of giving them two options to return a value. One would be with a button with text Select and the other would be a double click event. I assume the code would be the same between these two methods.  The query behind this dgv is:

SELECT CUSTNMBR,CUSTNAME FROM CUSTOMERS ORDER BY CUSTNMBR

This query is used to populate a data table(Customers) and then the datagridview(dgvCustomers) is populated using this data table.

What is the proper method to identify the cell to which the user is currently pointing and returning the customer number?
C#

Avatar of undefined
Last Comment
Eduard Ghergu

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Eduard Ghergu

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rwheeler23

ASKER
I got this to work using these three methods. First I call the lookup window by clicking a button.

        private void btnHistBatch_Click(object sender, EventArgs e)
        {
            using (frmLookupBatchIDs LookupBatchIDsForm = new frmLookupBatchIDs(batchNumber))
            {
                if (LookupBatchIDsForm.ShowDialog(this) == DialogResult.OK)
                {
                    LookupBatchIDsForm.Show();
                    LookupBatchIDsForm.Activate();
                    LookupBatchIDsForm.Focus();
                    batchNumber = LookupBatchIDsForm.CellValue;
                }
            }

            txtBatchID.Text = batchNumber;
        }

-----------------------------------------------------------------------------------------
I then populate a datagridview
----------------------------------------------------------------------------------------
        public frmLookupBatchIDs(string bachNumber)
        {
            InitializeComponent();

            DataTable batchIDs = new DataTable();
            int BatchRecords = DataAccess.GetListofSOPBatches(Dynamics.Globals.IntercompanyId, ref batchIDs);
            if (BatchRecords == 0)
            {
                MessageBox.Show("No invoice batches found");
            }

            dgvBatchIDs.ReadOnly = true;
            dgvBatchIDs.RowHeadersVisible = false;
            dgvBatchIDs.DataSource = batchIDs;

            dgvBatchIDs.Columns[0].Width = 250;
            dgvBatchIDs.Columns[0].HeaderText = "Batch IDs";
        }
--------------------------------------------------------------------------------------
I then use method to select the value.

--------------------------------------------------------------------------------------

        private void btnSelect_Click(object sender, EventArgs e)
        {
            int CurrentRow;
            int columnToReturn = 0;

            CurrentRow = dgvBatchIDs.CurrentCell.RowIndex;
            dgvBatchIDs.CurrentCell = dgvBatchIDs[columnToReturn, CurrentRow];
            CellValue = dgvBatchIDs.CurrentCell.Value.ToString();

            /* Retrieve Customer number */
            custValue = dgvBatchIDs[columnToReturn, CurrentRow].Value.ToString();

            DialogResult = DialogResult.OK;            

            return;
        }
--------------------------------------------------------------------------------------------

My question now is how does a value actually come back? I am using CellValue to return the value.
Is it simply
            CurrentRow = dgvBatchIDs.CurrentCell.RowIndex;
            dgvBatchIDs.CurrentCell = dgvBatchIDs[columnToReturn, CurrentRow];
            CellValue = dgvBatchIDs.CurrentCell.Value.ToString();

CurrentRow gets the current highlighted row
dgvBatchIDs.CurrentCell gets the value in the current cell
CellValue is then assigned that value?

I think I am over thinking this. I am still trying to get used C#.
Eduard Ghergu

Hi,
Great!
rwheeler23

ASKER
Thanks for your input.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Eduard Ghergu

Hi,
My pleasure! Please, let me know if you need more help.