Link to home
Get AccessLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

How to get C# lookup window to land on first matching record

I have this method that correctly displays a list of batch numbers. I would like to refine it by passing down a batch prefix and have the display begin at whatever that prefix is. I want the user to still be able to go forward and backward in the list.The data resides in an MSSQL table.Is this something that can added or is it best to use some third-party developer tool?

        private void DisplayBatches()
        {

            DataTable dataTable = new DataTable();

            /* dataTable = new DataTable(); */

            string commandText = "SELECT BACHNUMB AS 'Batch ID'," +
                                        "CASE WHEN BCHSOURC = 'GL_Clearing' THEN 'Clearing Entry' " +
                                             "WHEN BCHSOURC = 'GL_Normal' THEN 'General Entry' " +
                                             "WHEN BCHSOURC = 'Invoice Entry' THEN 'Invoice Entry' " +
                                             "WHEN BCHSOURC = 'IV_Trans' THEN 'Transction Entry' " +
                                             "WHEN BCHSOURC = 'IV_Trxent' THEN 'Transfer Entry' " +
                                             "WHEN BCHSOURC = 'ASMENT' THEN 'Assembly Entry' " +
                                             "WHEN BCHSOURC = 'PM_Payment' THEN 'Manual Checks' " +
                                             "WHEN BCHSOURC = 'PM_Trxent' THEN 'Payables Trx Entry' " +
                                             "WHEN BCHSOURC = 'Rcvg Trx' THEN 'Receivings Trx Entry' " +
                                             "WHEN BCHSOURC = 'Rcvg Trx Ivc' THEN 'Purchasing Invoice Entry' " +
                                             "WHEN BCHSOURC = 'RM_Cash' THEN 'Cash Receipts' " +
                                             "WHEN BCHSOURC = 'RM_Sales' THEN 'Transaction Entry' " +
                                             "WHEN BCHSOURC = 'Sales Entry' THEN 'Sales Transaction Entry' " +
                                             "WHEN BCHSOURC = 'XPM_Cchecks' THEN 'Computer Checks' " +
                                             "ELSE 'Unknown' END AS 'Origin'" +
                                             ",CASE WHEN NUMOFTRX = 0 THEN 'No Transactions' " +
                                             "ELSE CONVERT(CHAR(5),NUMOFTRX) +' Transactions' END AS 'Status'" +
                                             ",CASE WHEN BACHFREQ = 1 THEN 'Single Use' " +
                                             "ELSE 'Unknown' END AS 'Frequency' " +
                                             "FROM[dbo].[SY00500] " +
                                             "WHERE SERIES = 3 AND BCHSOURC='Sales Entry' " +
                                             "ORDER BY BACHNUMB";

            int recordCount = DataAccess.ExecuteDataSet(ref dataTable, Controller.Instance.Model.GPCompanyDatabase, CommandType.Text,commandText,null);

            /* MessageBox.Show("Record count : " + recordCount.ToString() + " - " + commandText); */

            dgvBatches.ReadOnly = true;
            dgvBatches.RowHeadersVisible = false;
            dgvBatches.DataSource = dataTable;

            dgvBatches.Columns[0].Width = 120;
            dgvBatches.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvBatches.Columns[0].DefaultCellStyle.Format = "0";
            dgvBatches.Columns[0].HeaderText = "Batch ID";

            dgvBatches.Columns[1].Width = 140;
            dgvBatches.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvBatches.Columns[1].DefaultCellStyle.Format = "0";
            dgvBatches.Columns[1].HeaderText = "Origin";

            dgvBatches.Columns[2].Width = 120;
            dgvBatches.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvBatches.Columns[2].DefaultCellStyle.Format = "0";
            dgvBatches.Columns[2].HeaderText = "Status";

            dgvBatches.Columns[3].Width = 100;
            dgvBatches.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvBatches.Columns[3].DefaultCellStyle.Format = "0";
            dgvBatches.Columns[3].HeaderText = "Frequency";

            dgvBatches.EditMode = DataGridViewEditMode.EditOnEnter;
            dgvBatches.Focus();
        }
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
I had answered this in a previous EE_PAQ - https:/Q_29180441.html#a43076495

In that I used a LINQ statement to find the match rather than using foreach loop as proposed by Kyle (which will also work).

-saige-
Avatar of rwheeler23

ASKER

Thank you both.
Where do I insert this into my code? This is what I have but where do I insert it?

            foreach (DataGridViewRow row in dgvBatches.Rows)
            {
                if (row.Cells[1].Value.ToString().Equals(batchID))
                {
                    rowIndex = row.Index;
                    dgvBatches.ClearSelection();
                    dgvBatches.Rows[rowIndex].Selected = true;
                    dgvBatches.FirstDisplayedScrollingRowIndex = rowIndex;
                    dgvBatches.Focus();
                    break;
                }
            }
Never mind. I traced this back to my original question and found the suggestion from IT_Saige. I am all set now.