Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm working on FIND and REPLACE functionality in datagridview.

Below is the result after working on it.

S.No
-----
CODE0001
CODE0002
CODE0003
CODE0004

Where S.No is column name.

When i FIND 0001 and ask to replace that with 1000, the result is,

S.No
-----
code1000
CODE0002
CODE0003
CODE0004

Find and Replace functionality is working but the text from UPPERCASE is changing to LOWERCASE.

[edit]
Code for Find and Replace:
C#
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
    if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
    {
        dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Replace(f.txtfind.Text.ToLower(), f.txtreplace.Text);
        bulidDataRow(i);
    }
}
Posted
Updated 5-Jan-16 23:29pm
v3
Comments
Richard MacCutchan 6-Jan-16 5:16am    
Your code has a bug. However, without seeing the code no one can guess where.
Member 8010354 6-Jan-16 5:25am    
Code moved to question
Richard MacCutchan 6-Jan-16 5:30am    
You are converting the text ToLower!
Herman<T>.Instance 6-Jan-16 5:38am    
Exactly. ToLower()

1 solution

Well yes...look at your code!
C#
... = ... .ToLower().Replace(f.txtfind.Text.ToLower(), f.txtreplace.Text);
You are lower casing your data before and during the Replace operation, so the result will always be lower case.

Consider using a Regex.Replace instead, which has a case insensitive version:
Regex.Replace Method (String, String, String, RegexOptions) (System.Text.RegularExpressions)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900