Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have searched and replaced the text in the datagridview..But the problem is that not all the rows of the string are replaced on the button click only some rows are replaced then if i click the button again then again some more rows are replaced.Now i want to replace the text in a string on a single button click.Here is my code.
C#
private void button7_Click_3(object sender, EventArgs e)
    {
        string filterBy;
        filterBy = "Stringtext Like '%" + textBox6.Text + "%'";
        ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = filterBy;
        int lent = ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter.Length;
        int rows = dataGridView1.Rows.Count;
        int col = dataGridView1.ColumnCount;
        //   for(int i=0
        //  for (int i = 0; i < rows/2; i++)
        //  {
        //   dataGridView1[2,i].Value=((dataGridView1[2, i].Value.ToString()).ToLower()).Replace(textBox6.Text.ToLower(), textBox7.Text);       //     ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = String.Format("Stringtext = '{0}'",textBox6.Text.Replace(
        //    dataGridView1[2, i].Value = ((dataGridView1[2, i].Value.ToString()).ToLower());
        //    dataGridView1[2, i].Value = dataGridView1[2, i].Value.ToString().Replace(textBox6.Text.ToLower(), textBox7.Text);
        //    dataGridView1[2, i].Style.ForeColor = Color.Red;
        //   }
        int i = 0;

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            dataGridView1[2,i].Value = (row.Cells[2].Value.ToString().ToLower()).Replace(textBox6.Text.ToLower(), textBox7.Text);
            i++;
            dataGridView1.Refresh();
        }

    }


Any ideas?
Posted
Updated 21-Jul-13 23:34pm
v2
Comments
Herman<T>.Instance 22-Jul-13 10:19am    
Are you using if (!Page.IsPostBack) in at least your Page_Load event?
zeeshan dar 22-Jul-13 23:44pm    
I am using winforms..I don't think it is necessary..
Herman<T>.Instance 23-Jul-13 9:59am    
true

1 solution

Hi
Change your code like this.Change first row and then colum dataGridView1[2,i].Value should be as dataGridView1[i,2].Value.
C#
foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            dataGridView1[i,2].Value = (row.Cells[2].Value.ToString().ToLower()).Replace(textBox6.Text.ToLower(), textBox7.Text);
            i++;
            dataGridView1.Refresh();
        }
 
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