VS 2008 C# Substring search Intellisense

rwheeler23
rwheeler23 used Ask the Experts™
on
I have this snippet of code that I want to proceed to the first record matching the keys as I type them. For example:
123 would go to the first record starting with 123
123A would go to the first record starting with 123A

The problem I have is that it ignores all characters. So when I type 123A it goes to 123 and not to the record that starts with 123A. I am not looking for only digits. How do I correct this? It would also be nice if the user could backspace to correct an error. Is that possible?
private void dgvAgenciesByJobs_KeyPress(object sender, KeyPressEventArgs e)
        {
            strSearch += e.KeyChar;
            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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Is dgvAgenciesByJobs a datagrid view winform?
Can you post some sample data? and dgvAgenciesByJobs code setup?
In the mean time:
Check that:
(1) string is correct.
dgvAgenciesByJobs[0, i].Value.ToString() is returning the string you need
Use Debug.WriteLine("Value =', dgvAgenciesByJobs[0, i].Value.ToString() )
(2) Check that strSearch  has the right string value you are after.
To handle backspace
 if (e.KeyChar == (char)Keys.Back)
        {
              if (strSearch.Length > 0)             strSearch = strSearch.SubString(0,strSearch.Length-1);
        }

Rest your code looks fine expect some logical flaw.
1. Just for testing you can add a record which starts with Characters and verify whether that is working or not.
2. You can take this in a variable for verify what values comes for 123A
bool temp =dgvAgenciesByJobs[0, i].Value.ToString().StartsWith(strSearch);


rwheeler23President

Author

Commented:
I see what is wrong but I do not see how to correct it. If you look at the output in the bottom left hand corner, when I first open this window:

Press 1 gets me 1001
Press 9 get me 1900CMP
It is landing on the first record and returning the entire value of the string instead of just the characters I have typed.
StringSearch.jpg
rwheeler23President

Author

Commented:
If I insert the line:

Debug.WriteLine("Value=",strSearch);

I type 1 I get 1
I type 9 I get 19
I type 0 I get 190
I type 0 I get 1900
I type C I get nothing and here is where it stops. It appears to be stripping away any non-digits. Why?
rwheeler23President

Author

Commented:
My mistake on this one. In the database all job number allow uppercase letters only. If I add the ToUpper command to the strSearch variable now it does exactly what it needs to do.
if (dgvAgenciesByJobs[0, i].Value != null && dgvAgenciesByJobs[0, i].Value.ToString().StartsWith(strSearch.ToUpper()))
                {
                    dgvAgenciesByJobs.Rows[i].Selected = true;
                    dgvAgenciesByJobs.CurrentCell = dgvAgenciesByJobs[0, i];
                    if (dgvAgenciesByJobs.Rows[i].Displayed == false)
                    {
                        dgvAgenciesByJobs.FirstDisplayedScrollingRowIndex = i;
                    }
                    return;
                }
            }

        }
 

Open in new window

rwheeler23President

Author

Commented:
Both answers help complete this code. Thanks.

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