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

asked on

c# lookup button does not work with only one letter.

I have this code that allows users to lookup batch IDs. If the user enters HM and clicks the lookup button, it goes to the first record that begins with HM. However, if the user enters only H and clicks the lookup button it does not go to the first record beginning with H. I do not see why. Batch ID is the first column in the SQL query and is the search column.

        public frmLookupBatch(string BatchID)
        {
            InitializeComponent();
            StartPosition = FormStartPosition.Manual;
            Location = new Point(500, 40);
           
            batchID = BatchID;

            try
            {
                DisplayBatches();
                ActiveControl = dgvBatches;

                //Make sure no cells are selected
                dgvBatches.ClearSelection();

                if (!string.IsNullOrEmpty(BatchID))
                {
                    //Search data grid for batch ID (contains)
                    rowIndex = dgvBatches.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["Batch ID"].Value.ToString().Contains(BatchID)).First().Index;

                    if (rowIndex >= 0)
                    {
                        //Select cell in the selected row
                        dgvBatches.CurrentCell = dgvBatches[0, rowIndex];

                        //Scroll to row of selected cell
                        dgvBatches.FirstDisplayedScrollingRowIndex = rowIndex;
                    }
                    else
                    {
                        //Batch ID contains match not found
                        MessageBox.Show("No such batch ID found : " + batchID);
                    }
                }
                else
                {
                    //Batch ID not supplied
                    MessageBox.Show("No batch ID specified");
                }
            }
            catch (Exception ex)
            {
                string eMsg = "Lookup Transfer-001A: ERROR: " + ex.Message;
                if (Model.StackTraceWanted) eMsg += "\n" + ex.StackTrace;
                if (Model.StackTraceWanted) MessageBox.Show(eMsg);
            }
        }
Avatar of rwheeler23
rwheeler23
Flag of United States of America image

ASKER

Please disregard this request. Changing Contains to StartsWith was what was needed.

rowIndex = dgvBatches.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["Batch ID"].Value.ToString().StartsWith(batchID)).First().Index;
ASKER CERTIFIED SOLUTION
Avatar of rwheeler23
rwheeler23
Flag of United States of America 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