Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a requirement where i need to do Find and Replace in datagridview for multiple pages - It's working fine.
The problem is, after i Find a specific word and replace it and again if i click on Find and Replace button, the earlier find and replace values are gone.

How to work on it?
Posted

1 solution

If you need to save the find and replace values you need to store the corresponding values to database. I think find and replace values are not storing in the database that's why you are not getting previous replaced values. If you don't want to save to database then you need to store the find and replace values in cookie and each post back you need to replace values of all the values in cookie.
 
Share this answer
 
Comments
Member 8010354 14-Jan-16 6:03am    
Thank you. But i'm working on windows application and i'm not fetching data from any database. I'm reading a file and displaying in datagridview.
In this case, how should i save it? shall i post my code so it'll be helpful?
Sri Nivas (Vasu) 14-Jan-16 6:50am    
Yes, that will be helpful.
Member 8010354 14-Jan-16 6:55am    
public string toFind = ""; public string toReplace = "";
private void btnFindandReplace_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.cmbColumnCombo.DataSource = cmbList;
f.ShowDialog();
toFind = f.txtfind.Text;
toReplace = f.txtreplace.Text;
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()))
{
if (!string.IsNullOrEmpty(f.txtfind.Text))
{
dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
}
}
}
}
Sri Nivas (Vasu) 14-Jan-16 7:32am    
Can you please provide the code for dataGridView1 binding ?
Member 8010354 14-Jan-16 8:24am    
BRWSE button code where i read the file into DataGridView:

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "Desktop";
openFileDialog1.Filter = "dat files (*.DAT)|*.DAT|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
FileName = openFileDialog1.FileName;
string text = System.IO.File.ReadAllText(FileName);
datfile = text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
//Added on 2015-12-02
maxRec = datfile.Length - 1;
PageCount = maxRec / pageSize;
LoadPage(MyFOrmat);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}

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