Using a datagridview as a lookup window

rwheeler23
rwheeler23 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Architect - Coder - Mentor
Commented:
Hi,
I'm not really sure that I've got your point, but I think that the CellClick event handling is what you need.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.cellclick?view=netframework-4.8
rwheeler23President

Author

Commented:
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 GherguArchitect - Coder - Mentor

Commented:
Hi,
Great!
rwheeler23President

Author

Commented:
Thanks for your input.
Eduard GherguArchitect - Coder - Mentor

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial